]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed bug which disallowed one side of a many-to-many
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 1 Sep 2009 22:42:35 +0000 (22:42 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 1 Sep 2009 22:42:35 +0000 (22:42 +0000)
bidirectional reference to declare itself as "viewonly"
[ticket:1507]

CHANGES
lib/sqlalchemy/orm/dependency.py
test/orm/test_relationships.py

diff --git a/CHANGES b/CHANGES
index f8786a1d0aa42d26c6b9494d7e8d75c3e2d1a34f..432c9e2a3c3e9867629297eef55eba67fc208891 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -374,7 +374,11 @@ CHANGES
     - Fixed bug whereby inheritance discriminator part of a
       composite primary key would fail on updates.
       Continuation of [ticket:1300].
-    
+
+    - Fixed bug which disallowed one side of a many-to-many 
+      bidirectional reference to declare itself as "viewonly"
+      [ticket:1507]
+
     - Fixed an obscure issue whereby a joined-table subclass
       with a self-referential eager load on the base class
       would populate the related object's "subclass" table with
@@ -442,7 +446,7 @@ CHANGES
       in query.join() which would fail to issue correctly
       if the query was against a pure SQL construct.
       [ticket:1522]
-      
+    
     - Fixed a somewhat hypothetical issue which would result
       in the wrong primary key being calculated for a mapper
       using the old polymorphic_union function - but this
index 407a04ae4dcf180f3abd2089361f89b43385cda5..8a4c5c098f758e7ff07198ce08fb51f7cd30d830 100644 (file)
@@ -140,7 +140,7 @@ class DependencyProcessor(object):
         
         """
         for r in self.prop._reverse_property:
-            if (r._dependency_processor, action, parent, child) in uowcommit.attributes:
+            if not r.viewonly and (r._dependency_processor, action, parent, child) in uowcommit.attributes:
                 return True
         return False
     
index e542f18856a9560284255fc85b0215601fcabe54..fad25ace5629b3a165fa3558684cbc6b4425489e 100644 (file)
@@ -1108,7 +1108,44 @@ class TypedAssociationTable(_base.MappedTest):
 
         assert t3.count().scalar() == 1
 
+class ViewOnlyM2MBackrefTest(_base.MappedTest):
+    @classmethod
+    def define_tables(cls, metadata):
+        Table("t1", metadata,
+            Column('id', Integer, primary_key=True, test_needs_autoincrement=True),
+            Column('data', String(40)))
+        Table("t2", metadata,
+            Column('id', Integer, primary_key=True, test_needs_autoincrement=True),
+            Column('data', String(40)),
+        )
+        Table("t1t2", metadata,
+            Column('t1id', Integer, ForeignKey('t1.id'), primary_key=True),
+            Column('t2id', Integer, ForeignKey('t2.id'), primary_key=True),
+        )
+    
+    @testing.resolve_artifact_names
+    def test_viewonly(self):
+        class A(_base.ComparableEntity):pass
+        class B(_base.ComparableEntity):pass
+        
+        mapper(A, t1, properties={
+            'bs':relation(B, secondary=t1t2, backref=backref('as_', viewonly=True))
+        })
+        mapper(B, t2)
+        
+        sess = create_session()
+        a1 = A()
+        b1 = B(as_=[a1])
 
+        sess.add(a1)
+        sess.flush()
+        eq_(
+            sess.query(A).first(), A(bs=[B(id=b1.id)])
+        )
+        eq_(
+            sess.query(B).first(), B(as_=[A(id=a1.id)])
+        )
+        
 class ViewOnlyOverlappingNames(_base.MappedTest):
     """'viewonly' mappings with overlapping PK column names."""