]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Ignore non-primary mappers within mutable instrumentation
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 12 Mar 2018 16:50:52 +0000 (12:50 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 12 Mar 2018 16:50:52 +0000 (12:50 -0400)
Fixed bug where using :meth:`.Mutable.associate_with` or
:meth:`.Mutable.as_mutable` in conjunction with a class that has non-
primary mappers set up with alternatively-named attributes would produce an
attribute error.  Since non-primary mappers are not used for persistence,
the mutable extension now excludes non-primary mappers from its
instrumentation steps.

Change-Id: I2630d9f771a171aece03181ccf9159885f68f25e
Fixes: #4215
doc/build/changelog/unreleased_12/4215.rst [new file with mode: 0644]
lib/sqlalchemy/ext/mutable.py
test/ext/test_mutable.py

diff --git a/doc/build/changelog/unreleased_12/4215.rst b/doc/build/changelog/unreleased_12/4215.rst
new file mode 100644 (file)
index 0000000..e658c5b
--- /dev/null
@@ -0,0 +1,12 @@
+.. change::
+    :tags: bug, orm
+    :tickets: 4215
+    :versions: 1.3.0b1
+
+    Fixed bug where using :meth:`.Mutable.associate_with` or
+    :meth:`.Mutable.as_mutable` in conjunction with a class that has non-
+    primary mappers set up with alternatively-named attributes would produce an
+    attribute error.  Since non-primary mappers are not used for persistence,
+    the mutable extension now excludes non-primary mappers from its
+    instrumentation steps.
+
index 955aa3ae192789b94962267e18b001645e2e3ea9..014cef3cce210ae954296fd0ccc8f6470c6141be 100644 (file)
@@ -566,6 +566,8 @@ class Mutable(MutableBase):
         """
 
         def listen_for_type(mapper, class_):
+            if mapper.non_primary:
+                return
             for prop in mapper.column_attrs:
                 if isinstance(prop.columns[0].type, sqltype):
                     cls.associate_with_attribute(getattr(class_, prop.key))
@@ -619,6 +621,8 @@ class Mutable(MutableBase):
             schema_event_check = False
 
         def listen_for_type(mapper, class_):
+            if mapper.non_primary:
+                return
             for prop in mapper.column_attrs:
                 if (
                         schema_event_check and
index 16fd52d97beaf8a585afc5d9141d635b01c1c619..654a85e7452855050082bad63746dacfe8079a9c 100644 (file)
@@ -801,6 +801,17 @@ class MutableWithScalarJSONTest(_MutableDictTestBase, fixtures.MappedTest):
         self._test_non_mutable()
 
 
+class MutableIncludeNonPrimaryTest(MutableWithScalarJSONTest):
+    @classmethod
+    def setup_mappers(cls):
+        foo = cls.tables.foo
+
+        mapper(Foo, foo)
+        mapper(Foo, foo, non_primary=True, properties={
+            "foo_bar": foo.c.data
+        })
+
+
 class MutableColumnCopyJSONTest(_MutableDictTestBase, fixtures.MappedTest):
 
     @classmethod
@@ -967,6 +978,17 @@ class MutableAssociationScalarPickleTest(_MutableDictTestBase,
               )
 
 
+class MutableAssocIncludeNonPrimaryTest(MutableAssociationScalarPickleTest):
+    @classmethod
+    def setup_mappers(cls):
+        foo = cls.tables.foo
+
+        mapper(Foo, foo)
+        mapper(Foo, foo, non_primary=True, properties={
+            "foo_bar": foo.c.data
+        })
+
+
 class MutableAssociationScalarJSONTest(_MutableDictTestBase,
                                        fixtures.MappedTest):