]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- [bug] Fixed issue in unit of work
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 4 May 2012 23:27:57 +0000 (19:27 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 4 May 2012 23:27:57 +0000 (19:27 -0400)
    whereby setting a non-None self-referential
    many-to-one relationship to None
    would fail to persist the change if the
    former value was not already loaded.
    [ticket:2477].

CHANGES
lib/sqlalchemy/orm/attributes.py
test/orm/test_unitofworkv2.py

diff --git a/CHANGES b/CHANGES
index 0c47aa66e6f2a6ac1a6ca6c69ad38a6e5c86a8fc..0be1dc951b34d789f5648b28a40006df9abaf1c8 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -71,6 +71,13 @@ CHANGES
     set of objects that weren't modified in 
     that sub-transaction.  [ticket:2452]
 
+  - [bug] Fixed issue in unit of work
+    whereby setting a non-None self-referential
+    many-to-one relationship to None
+    would fail to persist the change if the
+    former value was not already loaded.
+    [ticket:2477]. Also in 0.7.7
+
   - [bug] Fixed bug in relationship comparisons
     whereby calling unimplemented methods like
     SomeClass.somerelationship.like() would
index 7625ccead69b99cd81890cf23ff09b7948185f30..00d640066a47260e81f293f02ac4ef66ded02231 100644 (file)
@@ -627,7 +627,7 @@ class ScalarObjectAttributeImpl(ScalarAttributeImpl):
             if current is not None:
                 ret = [(instance_state(current), current)]
             else:
-                ret = []
+                ret = [(None, None)]
 
             if self.key in state.committed_state:
                 original = state.committed_state[self.key]
index d7e2150db5a102f56df19e5a7ac41e683e53e183..cfd36e696d10d44f360a50b833a0fbe079792716 100644 (file)
@@ -777,6 +777,31 @@ class SingleCycleTest(UOWTest):
                         lambda ctx: {'id':n1.id})
         )
 
+    def test_many_to_one_set_null_unloaded(self):
+        Node, nodes = self.classes.Node, self.tables.nodes
+
+        mapper(Node, nodes, properties={
+            'parent':relationship(Node, remote_side=nodes.c.id)
+        })
+        sess = create_session()
+        n1 = Node(data='n1')
+        n2 = Node(data='n2', parent=n1)
+        sess.add_all([n1, n2])
+        sess.flush()
+        sess.close()
+
+        n2 = sess.query(Node).filter_by(data='n2').one()
+        n2.parent = None
+        self.assert_sql_execution(
+            testing.db,
+            sess.flush,
+            CompiledSQL(
+                "UPDATE nodes SET parent_id=:parent_id WHERE "
+                "nodes.id = :nodes_id",
+                lambda ctx: {"parent_id":None, "nodes_id":n2.id}
+            )
+        )
+
     def test_cycle_rowswitch(self):
         Node, nodes = self.classes.Node, self.tables.nodes