]> 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:01:37 +0000 (18:01 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 14 Oct 2007 18:01:37 +0000 (18:01 +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 755f503de710ea5f0e6a541a4e5582ae5dbe24e7..70c875381668142786c8061a2f1671b8313434ed 100644 (file)
--- a/CHANGES
+++ b/CHANGES
       no PK columns, would not detect that the joined table had no PK.
     - fixed bugs in determining proper sync clauses from custom inherit
       conditions [ticket:769]
+    - backref remove object operation doesn't fail if the other-side 
+      collection doesn't contain the item, supports noload collections
+      [ticket:813]
+      
 - engine
     - fixed another occasional race condition which could occur
       when using pool with threadlocal setting
index 9f8a04db850fd40d7df1858d37532e327b205890..234826938899d9503f050e7f548d9ec725ee9c29 100644 (file)
@@ -643,7 +643,11 @@ class GenericBackrefExtension(AttributeExtension):
         if oldchild is child:
             return
         if oldchild is not None:
-            getattr(oldchild.__class__, self.key).remove(event, oldchild, obj)
+            try:
+                getattr(oldchild.__class__, self.key).remove(event, oldchild, obj)
+            except ValueError:
+                # supports 'noload' collections
+                pass
         if child is not None:
             getattr(child.__class__, self.key).append(event, child, obj)
 
index 42bc4d1ad896601b2764d680c2356e369e29407c..4bc6ff43e35e078fc2e0f662e17f58d745e1de5b 100644 (file)
@@ -776,7 +776,7 @@ class OneToManyTest(UnitOfWorkTest):
 
         ctx.current.delete(u)
         ctx.current.flush()
-
+    
     def testdoublerelation(self):
         m2 = mapper(Address, addresses)
         m = mapper(User, users, properties={
@@ -1154,6 +1154,31 @@ class ManyToOneTest(UnitOfWorkTest):
         u1 = ctx.current.query(User).get(u1.user_id)
         u2 = ctx.current.query(User).get(u2.user_id)
         assert a1.user is u2
+
+    def testbidirectional_noload(self):
+        mapper(User, users, properties={
+            'addresses':relation(Address, backref='user', lazy=None)
+        })
+        mapper(Address, addresses)
+
+        sess = ctx.current
+
+        # 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(UnitOfWorkTest):
     def setUpAll(self):