Please note: this project is inactive since early 2006

 
2.4.8 Association

Associations are a pratical shortvut for defining a relationship and its inverse in a single python statement.

Suppose that we want to design the two relationships already discussed above between entities Employee and Store:

  Employee <<-toEmployees------toStore-> Store

We could define the appropriate RToOne and RToMany objects in their respective entities. Now we can also define them like this:

model.entities = [ Entity('Employee'), Entity('Store') ]
model.associations = [
  Association('Employee', 'Store'),
  ]
(We've only left in the example the necessary declarations for demonstration -the full pymodel is exposed in 2.4.2).

This automatically creates the two relationships, along with the necessay foreign key.

IMPORTANT: Association objects always define a to-one association from the first entity to the second entity, and an inverse to-many relationship from the second entity to the first one.

Here is an equivalent declaration, where some of the defaults are explictly exposed:

  Association('Employee', 'Store',
              relations=['toStore', 'toEmployees'],
              multiplicity=[ [0, 1], [0, None] ],
              delete=['nullify', 'deny'])

Here again and as a general rule, we suggest that you provide at least the names and the multiplicity of both relationships, so that it is clear which one is the to-one/to-many relationship.

Now here are the defaults that Association uses. As you see, it uses the same defaults then RToOne and RToMany:

Prop.  Type  Default  Comment 
src string no default The source entity's name. This parameter is mandatory when creating a Association
dst string no default The destination entity's name. This parameter is mandatory when creating a Association
multiplicity sequence [ [0,1], [0,None] ] The multiplicity for each rel.
relations sequence ['to<Dst>', 'to<Src>s'] The names for the relationships
keys sequence [None, None] The two attributes'names that both relationships refer to as source/destination attributes
delete sequence [ RToOne.defaults['delete'], RToMany.defaults['delete'] ] the delete rule for each rel.
isClassProperty sequence [ RToOne.defaults['isClassProperty'], RToMany.defaults['isClassProperty'] ] Whether each rel. is a class property
joinSemantic sequence [ RToOne.defaults['joinSemantic'], RToMany.defaults['joinSemantic'] ] The join semantic for each rel.
displayLabel sequence [ RToOne.defaults['displayLabel'], RToMany.defaults['displayLabel'] ] the displayLabel for each rel.
doc sequence [ RToOne.defaults['doc'], RToMany.defaults['doc'] ] A comment assigned to each rel.

Last, an Association can be used to define a directional association (where only one of the two relationships is defined), by setting one of the relations to None, such as in:

  Association('Writer', 'Writer',
              relations=['pygmalion', None],
              delete=['nullify', None],
              keys=['FK_Writer_id', 'id']),

(extracted from Modeling/tests/testPackages/AuthorBooks/pymodel_AuthorBooks.py, corresponding to the model defined in 2.1.1)

Comments are welcome: Sebastien Bigaret / Modeling Home Page
Hosted by:SourceForge.net Logo