I believe that I may have found an error in dynamic relation backrefences. When the dynamic query is executed, it appears to fetch all of the rows in the referenced table instead of only the ones linked to the object from which the dynamic relation was obtained. The following test script illustrates the problem:
from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.orm.interfaces import MapperExtension class Parent(object): pass class Child(object): pass meta = ThreadLocalMetaData() Session = sessionmaker(autoflush=True, transactional=True) session = None class ThreadLocalSession(MapperExtension): def get_session(self): return session parents_table = Table( "parents", meta, Column("id", Integer, Sequence("parents_id_seq"), primary_key=True), Column("name", String(255), nullable=False)) mapper(Parent, parents_table) children_table = Table( "children", meta, Column("id", Integer, Sequence("children_id_seq"), primary_key=True), Column("name", String(255), nullable=False), Column("parent_id", Integer, ForeignKey("parents.id"), nullable=False)) # The mapper extension is necessary, otherwise the dynamic query returned by # Parent.children will not be associated with a session. mapper(Child, children_table, properties=dict( parent=relation(Parent, backref=backref("children", lazy="dynamic"))), extension=ThreadLocalSession()) engine = create_engine("sqlite:///:memory:", echo=False) meta.bind = engine session = Session(bind=engine) parents_table.create() for name in ["parent1", "parent2"]: parent = Parent() parent.name = name session.save(parent) session.flush() firstParent = session.query(Parent).get(1) secondParent = session.query(Parent).get(2) children_table.create() for name in ["child1", "child2", "child3", "child4"]: child = Child() child.name = name if name == "child1": child.parent = firstParent else: child.parent = secondParent session.save(child) session.flush() session.commit() session.clear() first_parent = session.query(Parent).get(1) second_parent = session.query(Parent).get(2) num_first_children = first_parent.children.count() num_second_children = second_parent.children.count() assert num_first_children == 1 and num_second_children == 3, \ "Children in first parent = %s, in second parent = %s" % \ (num_first_children, num_second_children) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---