From 31f26e561cc1aeb3475e6e662839b07a60412c36 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 1 Sep 2009 22:42:35 +0000 Subject: [PATCH] - Fixed bug which disallowed one side of a many-to-many bidirectional reference to declare itself as "viewonly" [ticket:1507] --- CHANGES | 8 +++++-- lib/sqlalchemy/orm/dependency.py | 2 +- test/orm/test_relationships.py | 37 ++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 3 deletions(-) diff --git a/CHANGES b/CHANGES index f8786a1d0a..432c9e2a3c 100644 --- 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 diff --git a/lib/sqlalchemy/orm/dependency.py b/lib/sqlalchemy/orm/dependency.py index 407a04ae4d..8a4c5c098f 100644 --- a/lib/sqlalchemy/orm/dependency.py +++ b/lib/sqlalchemy/orm/dependency.py @@ -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 diff --git a/test/orm/test_relationships.py b/test/orm/test_relationships.py index e542f18856..fad25ace56 100644 --- a/test/orm/test_relationships.py +++ b/test/orm/test_relationships.py @@ -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.""" -- 2.47.2