From: Mike Bayer Date: Fri, 16 Apr 2010 22:35:49 +0000 (-0400) Subject: - don't switch a delete to a non-delete during rowswitch, fixes [ticket:1772] X-Git-Tag: rel_0_6_0~7 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d72b327786d663b3954df345c6afc30e8e6f91fc;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - don't switch a delete to a non-delete during rowswitch, fixes [ticket:1772] --- diff --git a/lib/sqlalchemy/orm/unitofwork.py b/lib/sqlalchemy/orm/unitofwork.py index 756389fa7d..cbf45039e1 100644 --- a/lib/sqlalchemy/orm/unitofwork.py +++ b/lib/sqlalchemy/orm/unitofwork.py @@ -130,7 +130,9 @@ class UOWTransaction(object): def remove_state_actions(self, state): """remove pending actions for a state from the uowtransaction.""" - self.states[state] = (False, True) + isdelete = self.states[state][0] + + self.states[state] = (isdelete, True) def get_attribute_history(self, state, key, passive=True): """facade to attributes.get_state_history(), including caching of results.""" diff --git a/test/orm/test_unitofworkv2.py b/test/orm/test_unitofworkv2.py index 33e5f557e4..dfca1c05b3 100644 --- a/test/orm/test_unitofworkv2.py +++ b/test/orm/test_unitofworkv2.py @@ -653,3 +653,55 @@ class SingleCycleM2MTest(_base.MappedTest, testing.AssertsExecutionResults, Asse ), ) +class RowswitchAccountingTest(_base.MappedTest): + @classmethod + def define_tables(cls, metadata): + Table('parent', metadata, + Column('id', Integer, primary_key=True) + ) + Table('child', metadata, + Column('id', Integer, ForeignKey('parent.id'), primary_key=True) + ) + + @testing.resolve_artifact_names + def test_accounting_for_rowswitch(self): + class Parent(object): + def __init__(self, id): + self.id = id + self.child = Child() + class Child(object): + pass + + mapper(Parent, parent, properties={ + 'child':relationship(Child, uselist=False, cascade="all, delete-orphan", backref="parent") + }) + mapper(Child, child) + + sess = create_session(autocommit=False) + + p1 = Parent(1) + sess.add(p1) + sess.commit() + + sess.close() + p2 = Parent(1) + p3 = sess.merge(p2) + + old = attributes.get_history(p3, 'child')[2][0] + assert old in sess + + sess.flush() + + assert p3.child._sa_instance_state.session_id == sess.hash_key + assert p3.child in sess + + p4 = Parent(1) + p5 = sess.merge(p4) + + old = attributes.get_history(p5, 'child')[2][0] + assert old in sess + + sess.flush() + + +