]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- backref remove object operation doesn't fail if the other-side
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 14 Oct 2007 18:06:13 +0000 (18:06 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 14 Oct 2007 18:06:13 +0000 (18:06 +0000)
collection doesn't contain the item, supports noload collections
[ticket:813]

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

diff --git a/CHANGES b/CHANGES
index 53ce0e75eef923378b4eacc4a5a9e282fc91310b..7f1543e8685aeb8f688adb45d7706b3814dda2a3 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -9,6 +9,10 @@ CHANGES
 - Added partial index support for PostgreSQL. Use the postgres_where keyword
   on the Index.
 
+- backref remove object operation doesn't fail if the other-side
+  collection doesn't contain the item, supports noload collections
+  [ticket:813]
+
 - The IdentifierPreprarer's _requires_quotes test is now regex based.  Any
   out-of-tree dialects that provide custom sets of legal_characters or
   illegal_initial_characters will need to move to regexes or override
index 714b00376cb415532d4f47f73a062fea2a186707..4b64d52e31ded655d2d56b781036045ad1f94d3f 100644 (file)
@@ -520,7 +520,13 @@ class GenericBackrefExtension(interfaces.AttributeExtension):
         if oldchild is child:
             return
         if oldchild is not None:
-            getattr(oldchild.__class__, self.key).impl.remove(oldchild._state, obj, initiator)
+            # With lazy=None, there's no guarantee that the full collection is
+            # present when updating via a backref.
+            impl = getattr(oldchild.__class__, self.key).impl
+            try:                
+                impl.remove(oldchild._state, obj, initiator)
+            except (ValueError, KeyError, IndexError):
+                pass
         if child is not None:
             getattr(child.__class__, self.key).impl.append(child._state, obj, initiator)
 
index dcf7581fe5b19ec4d338cd205485cfafa5d00639..d1011068b6449f0cb4abe524c75a253e80c09ec0 100644 (file)
@@ -1334,6 +1334,31 @@ class ManyToOneTest(ORMTest):
         u1 = Session.query(User).get(u1.user_id)
         u2 = Session.query(User).get(u2.user_id)
         assert a1.user is u2
+
+    def test_bidirectional_noload(self):
+        mapper(User, users, properties={
+            'addresses':relation(Address, backref='user', lazy=None)
+        })
+        mapper(Address, addresses)
+
+        sess = Session()
+
+        # try it on unsaved objects
+        u1 = User()
+        a1 = Address()
+        a1.user = u1
+        sess.save(u1)
+        sess.flush()
+        sess.clear()
+
+        a1 = sess.query(Address).get(a1.address_id)
+
+        a1.user = None
+        sess.flush()
+        sess.clear()
+        assert sess.query(Address).get(a1.address_id).user is None
+        assert sess.query(User).get(u1.user_id).addresses == []
+
         
 class ManyToManyTest(ORMTest):
     metadata = tables.metadata