I encountered an odd issue today that I can't explain, and it seems like a bug.
I've checked 0.5.4p2 and 0.5.5, and the behavior is the same.

I wrote a test (add to test/orm/test_relationships.py) to demonstrate
it, which is below.
If I change the test from:
        for child in root.children:
            child.parent = new_root
to:
        new_root.children = root.children
or
        new_root.children.extend( root.children )

Then things work as expected.




class RelationTest7(_base.MappedTest):
    """Parent-child relationships"""

    @classmethod
    def define_tables(cls, metadata):
        Table("tableA", metadata,
              Column("id",Integer,primary_key=True),
              Column("data",String(20)),
              Column("parentid",Integer,ForeignKey("tableA.id")),
              test_needs_fk=True)

    @classmethod
    def setup_classes(cls):
        class A(_base.Entity):
            pass

    @classmethod
    @testing.resolve_artifact_names
    def setup_mappers(cls):
        mapper(A, tableA, properties={
            'children': relation(A,
              cascade="all,delete",
              backref=backref('parent', remote_side=[tableA.c.id]),
            ),
          }
        )

    @testing.resolve_artifact_names
    def test_pc1(self):
        session = create_session()
        root = session.query(A).filter(A.data=="root").one()
        new_root = session.query(A).filter(A.data=="newroot").one()

        assert len(root.children) == 100

        for child in root.children:
            child.parent = new_root

        session.flush()
        session.expunge_all()

        root = session.query(A).filter(A.data=="root").one()
        assert len(root.children) == 0
        assert root.children == []

    @testing.resolve_artifact_names
    def insert_data(self):
        session = create_session()
        root = A(data="root")
        root.parent = root
        for i in range(100):
          root.children.append( A() )
        session.add(root)

        new_root = A(data="newroot")
        new_root.parent = new_root
        session.add(new_root)

        session.flush()



-- 
Jon

--~--~---------~--~----~------------~-------~--~----~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to