]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
adjusted "blank out primary key" rule to check for "allow_null_pks" on target mapper...
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 7 Nov 2007 01:36:16 +0000 (01:36 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 7 Nov 2007 01:36:16 +0000 (01:36 +0000)
recent attributes.py change in r3695 that causes a value of "None" to register as part of the attribute history's
added_items() collection (i.e. since AttributeHistory compares against NO_VALUE instead of None).

CHANGES
lib/sqlalchemy/orm/sync.py
test/orm/relationships.py

diff --git a/CHANGES b/CHANGES
index e8b94b21cc930976b8d91b20b18765db99948ea4..b849557463f1ebe98ca874fa1deccdb376e1fed9 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -87,7 +87,7 @@ CHANGES
   - Deferred column attributes no longer trigger a load operation when the
     attribute is assigned to.  In those cases, the newly assigned value
     will be present in the flushes' UPDATE statement unconditionally.
-
+    
   - Fixed a truncation error when re-assigning a subset of a collection
     (obj.relation = obj.relation[1:]) [ticket:834]
 
index d858428611a02a78e397224e17c9d1f292c90fb9..5b5a9e43b1285839a99003b82d20409666181055 100644 (file)
@@ -118,7 +118,7 @@ class SyncRule(object):
         try:
             return self._dest_primary_key
         except AttributeError:
-            self._dest_primary_key = self.dest_mapper is not None and self.dest_column in self.dest_mapper.pks_by_table[self.dest_column.table]
+            self._dest_primary_key = self.dest_mapper is not None and self.dest_column in self.dest_mapper.pks_by_table[self.dest_column.table] and not self.dest_mapper.allow_null_pks
             return self._dest_primary_key
 
     def execute(self, source, dest, obj, child, clearkeys):
index d21121e1850dafbe797444c3506afd70bac7f55d..282d1cafc0252745bca75ccbc96030111cd727d9 100644 (file)
@@ -400,7 +400,40 @@ class RelationTest4(ORMTest):
             assert False
         except exceptions.AssertionError, e:
             assert str(e).startswith("Dependency rule tried to blank-out primary key column 'B.id' on instance ")
+            
+    def test_no_nullPK_sBtoA(self):
+        class A(object):pass
+        class B(object):pass
+        mapper(B, tableB, properties={
+            'a':relation(A, cascade="save-update")
+        })
+        mapper(A, tableA)
+        b1 = B()
+        b1.a = None
+        sess = create_session()
+        sess.save(b1)
+        try:
+            # this raises an error as of r3695.  in that rev, the attributes package was modified so that a 
+            # setting of "None" shows up as a change, which in turn fires off dependency.py and then triggers
+            # the rule.
+            sess.flush()
+            assert False
+        except exceptions.AssertionError, e:
+            assert str(e).startswith("Dependency rule tried to blank-out primary key column 'B.id' on instance ")
 
+    def test_nullPKsOK_sBtoA(self):
+        class A(object):pass
+        class B(object):pass
+        mapper(B, tableB, properties={
+            'a':relation(A, cascade="save-update")
+        }, allow_null_pks=True)
+        mapper(A, tableA)
+        b1 = B()
+        b1.a = None
+        sess = create_session()
+        sess.save(b1)
+        sess.flush()
+        
     def test_delete_cascade_BtoA(self):
         """test that the 'blank the PK' error doesnt get raised when the child is to be deleted as part of a 
         cascade"""