1. Éric Lemoine
  2. sqlalchemy

Commits

Mike Bayer  committed 1f33d16

- don't switch a delete to a non-delete during rowswitch, fixes [ticket:1772]

  • Participants
  • Parent commits b65cbfb
  • Branches default

Comments (0)

Files changed (2)

File lib/sqlalchemy/orm/unitofwork.py

View file
  • Ignore whitespace
     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."""

File test/orm/test_unitofworkv2.py

View file
  • Ignore whitespace
             ),
         )
     
+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()
+
+
+