[Sqlalchemy-tickets] Issue #3708: Inconsistent behavior when updating foreign key (zzzeek/sqlalchem
Brought to you by:
zzzeek
From: pkyosx <iss...@bi...> - 2016-05-07 03:26:57
|
New issue 3708: Inconsistent behavior when updating foreign key https://bitbucket.org/zzzeek/sqlalchemy/issues/3708/inconsistent-behavior-when-updating pkyosx: Suppose we have Parent and Child classes as One-To-Many relationship. If I move child from one parent to another, the order of changing foreign_key and foreign_object result in different behavior. Following are the briefing of reproducing steps. Attachments are runnable reproducing scripts. It still happens in 1.1.0b1.dev0 build. ``` #!python // Models class Parent(Base): __tablename__ = 'parent' id = Column(Integer, primary_key=True) children = relationship("Child", back_populates="parent") class Child(Base): __tablename__ = 'child' id = Column(Integer, primary_key=True) parent_id = Column(Integer, ForeignKey('parent.id')) parent = relationship("Parent", back_populates="children") // Initial Setting p1 = Parent() p2 = Parent() c = Child() c.parent = p1 s.commit() // Scenario1: Move c from p1 to p2 c.parent_id = p2.id c.parent = p2 assert c in p2.children, "this will fail" // Scenario 2: Move c from p1 to p2 c.parent = p2 c.parent_id = p2.id assert c in p2.children, "this will success" ``` |