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
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
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)
# 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]):
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:
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
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