]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed bug in :class:`.AbstractConcreteBase` extension where
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 13 Jul 2015 19:39:04 +0000 (15:39 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 13 Jul 2015 19:39:04 +0000 (15:39 -0400)
a column setup on the ABC base which had a different attribute
name vs. column name would not be correctly mapped on the final
base class.   The failure on 0.9 would be silent whereas on
1.0 it raised an ArgumentError, so may not have been noticed
prior to 1.0.
fixes #3480

doc/build/changelog/changelog_10.rst
lib/sqlalchemy/ext/declarative/api.py
lib/sqlalchemy/ext/declarative/base.py
test/ext/declarative/test_inheritance.py

index 8ac3d5844b0c84ab00e0cc207813a54cb5b9ce60..b5372be6833f530083712e41d54742e5c1183286 100644 (file)
 .. changelog::
     :version: 1.0.7
 
+    .. change::
+        :tags: bug, orm, declarative
+        :tickets: 3480
+
+        Fixed bug in :class:`.AbstractConcreteBase` extension where
+        a column setup on the ABC base which had a different attribute
+        name vs. column name would not be correctly mapped on the final
+        base class.   The failure on 0.9 would be silent whereas on
+        1.0 it raised an ArgumentError, so may not have been noticed
+        prior to 1.0.
+
     .. change::
         :tags: bug, orm
         :tickets: 3469
index 3d46bd4cb291b08c164673f04cc72fc3f7981962..dfc47ce959a039d9ca1945ada0a8de26b20b3129 100644 (file)
@@ -7,7 +7,7 @@
 """Public API functions and helpers for declarative."""
 
 
-from ...schema import Table, MetaData
+from ...schema import Table, MetaData, Column
 from ...orm import synonym as _orm_synonym, \
     comparable_property,\
     interfaces, properties, attributes
@@ -525,6 +525,17 @@ class AbstractConcreteBase(ConcreteBase):
                 mappers.append(mn)
         pjoin = cls._create_polymorphic_union(mappers)
 
+        # For columns that were declared on the class, these
+        # are normally ignored with the "__no_table__" mapping,
+        # unless they have a different attribute key vs. col name
+        # and are in the properties argument.
+        # In that case, ensure we update the properties entry
+        # to the correct column from the pjoin target table.
+        declared_cols = set(to_map.declared_columns)
+        for k, v in list(to_map.properties.items()):
+            if v in declared_cols:
+                to_map.properties[k] = pjoin.c[v.key]
+
         to_map.local_table = pjoin
 
         m_args = to_map.mapper_args_fn or dict
index 57eb54f63e773faf886efe9417f8b823e03f368d..57305748cc9e7cdfd2f22c6519633c8b9ff74d33 100644 (file)
@@ -463,7 +463,6 @@ class _MapperConfig(object):
 
     def _prepare_mapper_arguments(self):
         properties = self.properties
-
         if self.mapper_args_fn:
             mapper_args = self.mapper_args_fn()
         else:
index 3e6980190f63b90f3ffbb6ad35021948911a4d7e..274a6aa285ec643625945b53724ef669824f8ada 100644 (file)
@@ -1453,3 +1453,33 @@ class ConcreteExtensionConfigTest(
             "FROM actual_documents) AS pjoin"
         )
 
+    def test_column_attr_names(self):
+        """test #3480"""
+
+        class Document(Base, AbstractConcreteBase):
+            documentType = Column('documenttype', String)
+
+        class Offer(Document):
+            __tablename__ = 'offers'
+
+            id = Column(Integer, primary_key=True)
+            __mapper_args__ = {
+                'polymorphic_identity': 'offer'
+            }
+
+        configure_mappers()
+        session = Session()
+        self.assert_compile(
+            session.query(Document),
+            "SELECT pjoin.documenttype AS pjoin_documenttype, "
+            "pjoin.id AS pjoin_id, pjoin.type AS pjoin_type FROM "
+            "(SELECT offers.documenttype AS documenttype, offers.id AS id, "
+            "'offer' AS type FROM offers) AS pjoin"
+        )
+
+        self.assert_compile(
+            session.query(Document.documentType),
+            "SELECT pjoin.documenttype AS pjoin_documenttype FROM "
+            "(SELECT offers.documenttype AS documenttype, offers.id AS id, "
+            "'offer' AS type FROM offers) AS pjoin"
+        )