Changeset 43

Show
Ignore:
Timestamp:
02/09/07 11:32:30 (6 years ago)
Author:
ged
Message:

- completed the documentation of all relationships
- a tiny little bit of work toward making autoload work

Location:
elixir/trunk/elixir
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • elixir/trunk/elixir/entity.py

    r33 r43  
    4545        def __init__(cls, name, bases, dict_): 
    4646            # only process subclasses of Entity, not Entity itself 
    47             if bases[0] is object: return 
     47            if bases[0] is object: 
     48                return 
    4849             
    4950            # create the entity descriptor 
  • elixir/trunk/elixir/fields.py

    r38 r43  
    108108            desc.add_field(field) 
    109109 
    110  
    111 has_field   = Statement(HasField) 
     110has_field = Statement(HasField) 
    112111with_fields = Statement(WithFields) 
  • elixir/trunk/elixir/relationships.py

    r36 r43  
    55 
    66This module provides support for defining relationships between your Elixir  
    7 entities.  The supported relationship types are as follows: 
     7entities.  Elixir supports the following types of relationships: belongs_to_, 
     8has_one_, has_many_ and has_and_belongs_to_many_. For all types of  
     9relationships, you **must** specify the 'kind' of object you are relating to  
     10using the ``of_kind`` keyword argument.  
     11 
     12Additionally, if you want a bidirectionnal relationship, you should define the 
     13inverse relationship on the other entity explicitely (as opposed to  
     14SQLAlchemy's backref definitions). In non-ambiguous situations, Elixir will  
     15match relationships together automatically. If there are several relationships 
     16of the same type between two entities, Elixir is not able to determine which  
     17relationship is the inverse of which, so you have to disambiguate the  
     18situation by giving the name of the inverse relationship in the ``inverse``  
     19keyword argument. 
     20 
     21Here is a detailed explanation of each relation type: 
    822 
    923`belongs_to` 
    1024------------ 
     25 
    1126Describes the child's side of a parent-child relationship.  For example,  
    12 a `Pet` object may belong to its owner, who is a `Person.`  This could be 
     27a `Pet` object may belong to its owner, who is a `Person`.  This could be 
    1328expressed like so: 
    1429 
     
    1631 
    1732    class Pet(Entity): 
    18         belongs_to('owner', of_kind='Person', inverse='pets') 
    19  
    20 You must specify the 'kind' of object that you are relating to using the 
    21 of_kind keyword argument.  Additionally, if you plan on defining the other 
    22 side of the relationship, you should specify the name of the relationship 
    23 on the other side using the 'inverse' keyword argument. 
    24  
     33        belongs_to('owner', of_kind='Person') 
    2534 
    2635`has_one` 
    2736--------- 
    28 TODO. 
    29  
     37 
     38Describes the parent's side of a parent-child relationship when there is only 
     39one child.  For example, a `Car` object has one gear stick, which is  
     40represented as a `GearStick` object. This could be expressed like so: 
     41 
     42:: 
     43 
     44    class Car(Entity): 
     45        has_one('gear_stick', of_kind='GearStick', inverse='car') 
     46 
     47    class GearStick(Entity): 
     48        belongs_to('car', of_kind='Car') 
     49 
     50Note that an ``has_one`` relationship **cannot exist** without a corresponding  
     51``belongs_to`` relationship in the other way.  
    3052 
    3153`has_many` 
    3254---------- 
    33 TODO. 
    34  
     55 
     56Describes the parent's side of a parent-child relationship when there can be 
     57several children.  For example, a `Person` object has many children, each of 
     58them being a `Person`. This could be expressed like so: 
     59 
     60:: 
     61 
     62    class Person(Entity): 
     63        belongs_to('parent', of_kind='Person') 
     64        has_many('children', of_kind='Person') 
     65 
     66Note that an ``has_many`` relationship **cannot exist** without a  
     67corresponding ``belongs_to`` relationship in the other way.  
    3568 
    3669`has_and_belongs_to_many` 
    3770------------------------- 
    38 TODO. 
     71 
     72Describes a relationship in which one kind of entity can be related to several 
     73objects of the other kind but the objects of that other kind can be related to 
     74several objects of the first kind.  For example, an `Article` can have several 
     75tags, but the same `Tag` can be used on several articles. 
     76 
     77:: 
     78 
     79    class Article(Entity): 
     80        has_and_belongs_to_many('tags', of_kind='Tag') 
     81 
     82    class Tag(Entity): 
     83        has_and_belongs_to_many('articles', of_kind='Article') 
     84 
     85Note that you don't necessarily need to define the inverse relationship.  In 
     86our example, even though we want tags to be usables on several articles, we  
     87might not be interested in which articles correspond to a particular tag.  In 
     88that case, we could have omitted the `Tag` side of the relationship. 
    3989''' 
    4090 
     
    73123        self.property = None # sqlalchemy property 
    74124         
     125        #TODO: unused for now 
    75126        self.args = args 
    76127        self.kwargs = kwargs 
     
    79130        self.entity._descriptor.relationships[self.name] = self 
    80131     
    81     def create_keys(self): 
     132    def create_keys(self, autoload=False): 
    82133        ''' 
    83134        Subclasses (ie. concrete relationships) may override this method to  
     
    187238     
    188239     
    189     def create_keys(self): 
     240    def create_keys(self, autoload=False): 
    190241        ''' 
    191242        Find all primary keys on the target and create foreign keys on the  
     
    198249        if self.foreign_key: 
    199250            self.foreign_key = [source_desc.fields[k] 
    200                                     for k in self.foreign_key  
    201                                         if isinstance(k, basestring)] 
     251                                   for k in self.foreign_key  
     252                                       if isinstance(k, basestring)] 
    202253            return 
    203254         
     
    254305    uselist = False 
    255306 
    256     def create_keys(self): 
     307    def create_keys(self, autoload=False): 
    257308        # make sure the inverse is set up because it creates the 
    258309        # foreign key we'll need