]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- added "remote_side" argument to relation(), used only with self-referential
authorMike Bayer <mike_mp@zzzcomputing.com>
Sat, 2 Dec 2006 05:59:50 +0000 (05:59 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sat, 2 Dec 2006 05:59:50 +0000 (05:59 +0000)
mappers to force the direction of the parent/child relationship.  replaces
the usage of the "foreignkey" parameter for "switching" the direction;
while "foreignkey" can still be used to "switch" the direction of a parent/
child relationship, this usage is deprecated; "foreignkey" should always
indicate the actual foreign key columns from now on.

CHANGES
lib/sqlalchemy/orm/properties.py
lib/sqlalchemy/orm/sync.py
lib/sqlalchemy/topological.py
test/orm/cycles.py

diff --git a/CHANGES b/CHANGES
index f5ea33a5d330bd38bb5298d41dac36b7dc499254..8e013d73a87df4afa08020dc285cc9d277d702b9 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -10,6 +10,13 @@ an instance is detected to be already in the session.
 selects; only one selectable to an IN is allowed now (make a union yourself
 if union is needed; explicit better than implicit, dont guess, etc.)
 - improved support for disabling save-update cascade via cascade="none" etc.
+- added "remote_side" argument to relation(), used only with self-referential
+mappers to force the direction of the parent/child relationship.  replaces
+the usage of the "foreignkey" parameter for "switching" the direction;
+while "foreignkey" can still be used to "switch" the direction of a parent/
+child relationship, this usage is deprecated; "foreignkey" should always 
+indicate the actual foreign key columns from now on.
+
 0.3.1
 - Engine/Pool:
   - some new Pool utility classes, updated docs
index 8379a1d1f0b2cb7642dcaea29aaebaa8c56fe28c..959c7675e025b9448b853ad2acce4fcbb1045093 100644 (file)
@@ -68,7 +68,7 @@ mapper.ColumnProperty = ColumnProperty
 class PropertyLoader(StrategizedProperty):
     """describes an object property that holds a single item or list of items that correspond
     to a related database table."""
-    def __init__(self, argument, secondary, primaryjoin, secondaryjoin, foreignkey=None, uselist=None, private=False, association=None, order_by=False, attributeext=None, backref=None, is_backref=False, post_update=False, cascade=None, viewonly=False, lazy=True, collection_class=None, passive_deletes=False):
+    def __init__(self, argument, secondary, primaryjoin, secondaryjoin, foreignkey=None, uselist=None, private=False, association=None, order_by=False, attributeext=None, backref=None, is_backref=False, post_update=False, cascade=None, viewonly=False, lazy=True, collection_class=None, passive_deletes=False, remote_side=None):
         self.uselist = uselist
         self.argument = argument
         self.secondary = secondary
@@ -81,6 +81,7 @@ class PropertyLoader(StrategizedProperty):
         self.foreignkey = util.to_set(foreignkey)
         self.collection_class = collection_class
         self.passive_deletes = passive_deletes
+        self.remote_side = util.to_set(remote_side)
         
         if cascade is not None:
             self.cascade = mapperutil.CascadeOptions(cascade)
@@ -238,11 +239,18 @@ class PropertyLoader(StrategizedProperty):
             # for a self referential mapper, if the "foreignkey" is a single or composite primary key,
             # then we are "many to one", since the remote site of the relationship identifies a singular entity.
             # otherwise we are "one to many".
-            for f in self.foreignkey:
-                if not f.primary_key:
-                    return sync.ONETOMANY
+            if self.remote_side is not None and len(self.remote_side):
+                for f in self.foreignkey:
+                    if f in self.remote_side:
+                        return sync.ONETOMANY
+                else:
+                    return sync.MANYTOONE
             else:
-                return sync.MANYTOONE
+                for f in self.foreignkey:
+                    if not f.primary_key:
+                        return sync.ONETOMANY
+                else:
+                    return sync.MANYTOONE
         elif len([c for c in self.foreignkey if self.mapper.unjoined_table.corresponding_column(c, False) is not None]):
             return sync.ONETOMANY
         elif len([c for c in self.foreignkey if self.parent.unjoined_table.corresponding_column(c, False) is not None]):
index 156033868cf2b8bfd1cde454b9f6802f4c5d45ea..774853dd4350dfc9cf254c934ec763f92aea3d01 100644 (file)
@@ -51,8 +51,14 @@ class ClauseSynchronizer(object):
                     elif binary.right.primary_key:
                         source_column = binary.right
                         dest_column = binary.left
+                    elif binary.left in foreignkey:
+                        dest_column = binary.left
+                        source_column = binary.right
+                    elif binary.right in foreignkey:
+                        dest_column = binary.right
+                        source_column = binary.left
                     else:
-                        raise exceptions.ArgumentError("Can't locate a primary key column in self-referential equality clause '%s'" % str(binary))
+                        raise exceptions.ArgumentError("Can't figure out which column is source/dest in join clause '%s'" % str(binary))
                 # for other relationships we are more flexible
                 # and go off the 'foreignkey' property
                 elif binary.left in foreignkey:
index 5bd691f311c421d3cd7ccf60c22403b8546eef50..9d8c7280bdb3fb0bf8b3b1a2243595da13a2176c 100644 (file)
@@ -116,7 +116,8 @@ class _EdgeCollection(object):
                 yield (parent, child)
     def __str__(self):
         return repr(list(self))
-        
+    def __repr__(self):
+        return repr(list(self))
         
 class QueueDependencySorter(object):
     """topological sort adapted from wikipedia's article on the subject.  it creates a straight-line
index aa7a26711692fdb38e5072e08f289b2b0418e726..b5ad6ce332ce27803e793d48ff9f42e8899b9124 100644 (file)
@@ -118,7 +118,36 @@ class SelfReferentialTest(AssertMixin):
             assert False
         except exceptions.ArgumentError:
             assert True
-            
+
+class SelfReferentialNoPKTest(AssertMixin):
+    def setUpAll(self):
+        global table, meta
+        meta = BoundMetaData(testbase.db)
+        table = Table('item', meta,
+           Column('id', Integer, primary_key=True),
+           Column('uuid', String(32), unique=True, nullable=False),
+           Column('parent_uuid', String(32), ForeignKey('item.uuid'), nullable=True),
+        )
+        meta.create_all()
+    def tearDown(self):
+        table.delete().execute()
+    def tearDownAll(self):
+        meta.drop_all()
+    def testbasic(self):
+        class TT(object):
+            def __init__(self):
+                self.uuid = hex(id(self))
+        mapper(TT, table, properties={'children':relation(TT, remote_side=[table.c.parent_uuid], backref=backref('parent', remote_side=[table.c.uuid]))})
+        s = create_session()
+        t1 = TT()
+        t1.children.append(TT())
+        t1.children.append(TT())
+        s.save(t1)
+        s.flush()
+        s.clear()
+        t = s.query(TT).get_by(id=t1.id)
+        assert t.children[0].parent_uuid == t1.uuid
+        
 class InheritTestOne(AssertMixin):
     def setUpAll(self):
         global parent, child1, child2, meta