From: Mike Bayer Date: Sat, 2 Dec 2006 05:59:50 +0000 (+0000) Subject: - added "remote_side" argument to relation(), used only with self-referential X-Git-Tag: rel_0_3_2~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a97a1fcad41a3cbcbc7d3957b6229f39258fd7ed;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - 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. --- diff --git a/CHANGES b/CHANGES index f5ea33a5d3..8e013d73a8 100644 --- 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 diff --git a/lib/sqlalchemy/orm/properties.py b/lib/sqlalchemy/orm/properties.py index 8379a1d1f0..959c7675e0 100644 --- a/lib/sqlalchemy/orm/properties.py +++ b/lib/sqlalchemy/orm/properties.py @@ -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]): diff --git a/lib/sqlalchemy/orm/sync.py b/lib/sqlalchemy/orm/sync.py index 156033868c..774853dd43 100644 --- a/lib/sqlalchemy/orm/sync.py +++ b/lib/sqlalchemy/orm/sync.py @@ -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: diff --git a/lib/sqlalchemy/topological.py b/lib/sqlalchemy/topological.py index 5bd691f311..9d8c7280bd 100644 --- a/lib/sqlalchemy/topological.py +++ b/lib/sqlalchemy/topological.py @@ -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 diff --git a/test/orm/cycles.py b/test/orm/cycles.py index aa7a267116..b5ad6ce332 100644 --- a/test/orm/cycles.py +++ b/test/orm/cycles.py @@ -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