summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2006-12-02 05:59:50 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2006-12-02 05:59:50 +0000
commita97a1fcad41a3cbcbc7d3957b6229f39258fd7ed (patch)
tree56f6d2b1a60da2376af6cae407074da786f3bf98 /lib
parent0ab287b0394d4d5b42feb23267c3a589f1e9d438 (diff)
downloadsqlalchemy-a97a1fcad41a3cbcbc7d3957b6229f39258fd7ed.tar.gz
- 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.
Diffstat (limited to 'lib')
-rw-r--r--lib/sqlalchemy/orm/properties.py18
-rw-r--r--lib/sqlalchemy/orm/sync.py8
-rw-r--r--lib/sqlalchemy/topological.py3
3 files changed, 22 insertions, 7 deletions
diff --git a/lib/sqlalchemy/orm/properties.py b/lib/sqlalchemy/orm/properties.py
index 8379a1d1f..959c7675e 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 156033868..774853dd4 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 5bd691f31..9d8c7280b 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