]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
docs/examples/unittests for remote_side
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 8 Dec 2006 20:50:48 +0000 (20:50 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 8 Dec 2006 20:50:48 +0000 (20:50 +0000)
doc/build/content/adv_datamapping.txt
examples/adjacencytree/basic_tree.py
examples/adjacencytree/byroot_tree.py
examples/backref/backref_tree.py
test/orm/cycles.py

index df084eb2522b872daf7a3bda32ea1850a2a523e4..ffe09e06d1cab9ee421eaf974738e384c2b15529 100644 (file)
@@ -260,7 +260,8 @@ Keyword options to the `relation` function include:
 * secondary - for a many-to-many relationship, specifies the intermediary table.
 * primaryjoin - a ClauseElement that will be used as the primary join of this child object against the parent object, or in a many-to-many relationship the join of the primary object to the association table.  By default, this value is computed based on the foreign key relationships of the parent and child tables (or association table).
 * secondaryjoin - a ClauseElement that will be used as the join of an association table to the child object.  By default, this value is computed based on the foreign key relationships of the association and child tables.
-* foreignkey - a single Column, or list of Columns, specifying which column(s) in this relationship are "foreign", i.e. which column refers to the remote object.  This value is automatically determined in most cases from the primary join condition, by looking at which column in the join condition contains a `ForeignKey` pointing to the other column in an equality expression.  Specifying it here can override the normal foreign key properties of the join condition, which is useful for self-referential table relationships, join conditions where a `ForeignKey` is not present, or where the same column might appear on both sides of the join condition.
+* remote_side - used for self-referential relationships, indicates the column or list of columns that form the "remote side" of the relationship.
+* foreignkey - deprecated.  As of 0.3.2, use remote_side to indicate the direction of self-referential relationships.  For ForeignKey specification, this field is only partially functional (i.e. does not work for many-to-many relationships).  a new field "foreign_keys" will be added in a future release to implement this functionality.
 * uselist - a boolean that indicates if this property should be loaded as a list or a scalar.  In most cases, this value is determined based on the type and direction of the relationship - one to many forms a list, many to one forms a scalar, many to many is a list.  If a scalar is desired where normally a list would be present, such as a bi-directional one-to-one relationship, set uselist to False.
 * private - setting `private=True` is the equivalent of setting `cascade="all, delete-orphan"`, and indicates the lifecycle of child objects should be contained within that of the parent.   See the example in [datamapping_relations_cycle](rel:datamapping_relations_lifecycle).
 * backref - indicates the name of a property to be placed on the related mapper's class that will handle this relationship in the other direction, including synchronizing the object attributes on both sides of the relation.  Can also point to a `backref()` construct for more configurability.  See [datamapping_relations_backreferences](rel:datamapping_relations_backreferences).
@@ -645,18 +646,18 @@ A self-referential mapper where there is more than one relationship on the table
                             TreeNode, 
                             primaryjoin=trees.c.parent_node_id==trees.c.node_id
                             cascade="all",
-                            backref=backref("parent", foreignkey=trees.c.node_id)
+                            backref=backref("parent", remote_side=[trees.c.node_id])
                          ),
             'root' : relation(
                     TreeNode,
                     primaryjoin=trees.c.root_node_id=trees.c.node_id, 
-                    foreignkey=trees.c.node_id,
+                    remote_side=[trees.c.node_id],
                     uselist=False
                 )
             }
         )
         
-The "root" property on a TreeNode is a many-to-one relationship.  By default, a self-referential mapper declares relationships as one-to-many, so the extra parameter `foreignkey`, pointing to a column or list of columns on the remote side of a relationship, is needed to indicate a "many-to-one" self-referring relationship.
+The "root" property on a TreeNode is a many-to-one relationship.  By default, a self-referential mapper declares relationships as one-to-many, so the extra parameter `remote_side`, pointing to a column or list of columns on the remote side of a relationship, is needed to indicate a "many-to-one" self-referring relationship (note the previous keyword argument `foreignkey` is deprecated).
 Both TreeNode examples above are available in functional form in the `examples/adjacencytree` directory of the distribution.    
 
 ### Result-Set Mapping {@name=resultset}
index f05f3c3a78471820c4142629b702192f200062c0..3f4f64e3f9c2a8bac3039d9eeda0ae4b4dc92a2b 100644 (file)
@@ -47,7 +47,7 @@ mapper(TreeNode, trees, properties=dict(
     id=trees.c.node_id,
     name=trees.c.node_name,
     parent_id=trees.c.parent_node_id,
-    children=relation(TreeNode, cascade="all", backref=backref("parent", foreignkey=trees.c.node_id), collection_class=NodeList),
+    children=relation(TreeNode, cascade="all", backref=backref("parent", remote_side=[trees.c.node_id]), collection_class=NodeList),
 ))
 
 print "\n\n\n----------------------------"
index c2753f24918baed026b9ef499a085f878a8f12f0..ccd2646a4bab52ab0f1d7bd6a840dafdad36e2de 100644 (file)
@@ -127,7 +127,7 @@ mapper(TreeNode, trees, properties=dict(
     name=trees.c.node_name,
     parent_id=trees.c.parent_node_id,
     root_id=trees.c.root_node_id,
-    root=relation(TreeNode, primaryjoin=trees.c.root_node_id==trees.c.node_id, foreignkey=trees.c.node_id, lazy=None, uselist=False),
+    root=relation(TreeNode, primaryjoin=trees.c.root_node_id==trees.c.node_id, remote_side=trees.c.node_id, lazy=None, uselist=False),
     children=relation(TreeNode, primaryjoin=trees.c.parent_node_id==trees.c.node_id, lazy=None, uselist=True, cascade="delete,save-update", collection_class=NodeList),
     data=relation(mapper(TreeData, treedata, properties=dict(id=treedata.c.data_id)), cascade="delete,delete-orphan,save-update", lazy=False)
     
index d217b22f1cf22c4806b8f4d9a4fe4a14923c4a77..5f17396731056ffa32743364223a6c0adf40f6a4 100644 (file)
@@ -19,7 +19,7 @@ table.create()
 
 mapper(Tree, table,
               properties={
-                'childs':relation(Tree, foreignkey=table.c.father_id, primaryjoin=table.c.father_id==table.c.id,  backref=backref('father', foreignkey=table.c.id))},
+                'childs':relation(Tree, remote_side=table.c.father_id, primaryjoin=table.c.father_id==table.c.id,  backref=backref('father', remote_side=table.c.id))},
             )
 
 root = Tree('root')
index 7f7e1ada94d0581e8ca3394a53ebba5923a28ca6..eef1e6e0967638321a83d4bca7d41d3a3907d077 100644 (file)
@@ -43,7 +43,7 @@ class SelfReferentialTest(AssertMixin):
             pass
         m1 = mapper(C1, t1, properties = {
             'c1s':relation(C1, cascade="all"),
-            'parent':relation(C1, primaryjoin=t1.c.parent_c1==t1.c.c1, foreignkey=t1.c.c1, lazy=True, uselist=False)
+            'parent':relation(C1, primaryjoin=t1.c.parent_c1==t1.c.c1, remote_side=t1.c.c1, lazy=True, uselist=False)
         })
         a = C1('head c1')
         a.c1s.append(C1('another c1'))
@@ -64,7 +64,7 @@ class SelfReferentialTest(AssertMixin):
         class C1(Tester):
             pass
         mapper(C1, t1, properties={
-            'parent':relation(C1, primaryjoin=t1.c.parent_c1==t1.c.c1, foreignkey=t1.c.c1)
+            'parent':relation(C1, primaryjoin=t1.c.parent_c1==t1.c.c1, remote_side=t1.c.c1)
         })
         sess = create_session()
         c1 = C1()
@@ -365,8 +365,8 @@ class OneToManyManyToOneTest(AssertMixin):
 
         Ball.mapper = mapper(Ball, ball)
         Person.mapper = mapper(Person, person, properties= dict(
-         balls = relation(Ball.mapper, primaryjoin=ball.c.person_id==person.c.id, foreignkey=ball.c.person_id),
-         favorateBall = relation(Ball.mapper, primaryjoin=person.c.favorite_ball_id==ball.c.id, foreignkey=person.c.favorite_ball_id),
+         balls = relation(Ball.mapper, primaryjoin=ball.c.person_id==person.c.id, remote_side=ball.c.person_id),
+         favorateBall = relation(Ball.mapper, primaryjoin=person.c.favorite_ball_id==ball.c.id, remote_side=person.c.favorite_ball_id),
          )
         )
 
@@ -392,8 +392,8 @@ class OneToManyManyToOneTest(AssertMixin):
 
         Ball.mapper = mapper(Ball, ball)
         Person.mapper = mapper(Person, person, properties= dict(
-         balls = relation(Ball.mapper, primaryjoin=ball.c.person_id==person.c.id, foreignkey=ball.c.person_id, post_update=False, private=True),
-         favorateBall = relation(Ball.mapper, primaryjoin=person.c.favorite_ball_id==ball.c.id, foreignkey=person.c.favorite_ball_id, post_update=True),
+         balls = relation(Ball.mapper, primaryjoin=ball.c.person_id==person.c.id, remote_side=ball.c.person_id, post_update=False, private=True),
+         favorateBall = relation(Ball.mapper, primaryjoin=person.c.favorite_ball_id==ball.c.id, remote_side=person.c.favorite_ball_id, post_update=True),
          )
         )
 
@@ -496,8 +496,8 @@ class OneToManyManyToOneTest(AssertMixin):
 
         Ball.mapper = mapper(Ball, ball)
         Person.mapper = mapper(Person, person, properties= dict(
-         balls = relation(Ball.mapper, primaryjoin=ball.c.person_id==person.c.id, foreignkey=ball.c.person_id, private=True, post_update=True, backref='person'),
-         favorateBall = relation(Ball.mapper, primaryjoin=person.c.favorite_ball_id==ball.c.id, foreignkey=person.c.favorite_ball_id),
+         balls = relation(Ball.mapper, primaryjoin=ball.c.person_id==person.c.id, remote_side=ball.c.person_id, private=True, post_update=True, backref='person'),
+         favorateBall = relation(Ball.mapper, primaryjoin=person.c.favorite_ball_id==ball.c.id, remote_side=person.c.favorite_ball_id),
          )
         )
 
@@ -651,19 +651,19 @@ class SelfReferentialPostUpdateTest(AssertMixin):
                 primaryjoin=node_table.c.id==node_table.c.parent_id,
                 lazy=True,
                 cascade="all",
-                backref=backref("parent", primaryjoin=node_table.c.parent_id==node_table.c.id, foreignkey=node_table.c.id)
+                backref=backref("parent", primaryjoin=node_table.c.parent_id==node_table.c.id, remote_side=node_table.c.id)
             ),
             'prev_sibling': relation(
                 Node,
                 primaryjoin=node_table.c.prev_sibling_id==node_table.c.id,
-                foreignkey=node_table.c.id,
+                remote_side=node_table.c.id,
                 lazy=True,
                 uselist=False
             ),
             'next_sibling': relation(
                 Node,
                 primaryjoin=node_table.c.next_sibling_id==node_table.c.id,
-                foreignkey=node_table.c.id,
+                remote_side=node_table.c.id,
                 lazy=True,
                 uselist=False,
                 post_update=True