]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed bug whereby columns on a mixin wouldn't propagate
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 28 Sep 2010 16:43:36 +0000 (12:43 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 28 Sep 2010 16:43:36 +0000 (12:43 -0400)
correctly to a single-table inheritance scheme where
the attribute name is different than that of the column.
[ticket:1930].   Note [ticket:1931] which is the same
issue for joined inh, not yet resolved.

CHANGES
lib/sqlalchemy/ext/declarative.py
lib/sqlalchemy/orm/attributes.py
test/ext/test_declarative.py

diff --git a/CHANGES b/CHANGES
index d43732e4681bf1a1e60b9fb0eb78141e66dd8f00..38fd7443bf4f0eb80f3e78459b9bbe7413b00d71 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -143,7 +143,13 @@ CHANGES
      Same thing, but moving there since it is more of a
      "marker" that's specific to declararative, 
      not just an attribute technique.  [ticket:1915]
-     
+
+   - Fixed bug whereby columns on a mixin wouldn't propagate
+     correctly to a single-table inheritance scheme where
+     the attribute name is different than that of the column.
+     [ticket:1930].   Note [ticket:1931] which is the same
+     issue for joined inh, not yet resolved.
+          
 - engine
    
    - Fixed a regression in 0.6.4 whereby the change that
index cbda848e8060a899ea9e24870971006e5f4449c4..211b6724a70aba3d50de50cdf52f240210aa6880 100755 (executable)
@@ -938,7 +938,10 @@ def _as_declarative(cls, classname, dict_):
                         "on declarative mixin classes. ")
                     if name not in dict_ and not (
                             '__table__' in dict_ and 
-                            name in dict_['__table__'].c
+                            # TODO: coverage here when obj.name is not 
+                            # None and obj.name != name and obj.name in 
+                            # table.c
+                            (obj.name or name) in dict_['__table__'].c
                             ):
                         potential_columns[name] = \
                                 column_copies[obj] = \
@@ -960,7 +963,7 @@ def _as_declarative(cls, classname, dict_):
 
     # apply inherited columns as we should
     for k, v in potential_columns.items():
-        if tablename or k not in parent_columns:
+        if tablename or (v.name or k) not in parent_columns:
             dict_[k] = v
             
     if inherited_table_args and not tablename:
@@ -1087,7 +1090,7 @@ def _as_declarative(cls, classname, dict_):
                     "Can't place __table_args__ on an inherited class "
                     "with no table."
                     )
-        
+            
             # add any columns declared here to the inherited table.
             for c in cols:
                 if c.primary_key:
index b56de5f05dc93a07a0d8a106fd6450b697e27a43..ddaf62c7f17d2ebcc4de75d1cbcf053e6df19f32 100644 (file)
@@ -399,7 +399,7 @@ class AttributeImpl(object):
 
             # Return a new, empty value
             return self.initialize(state, dict_)
-
+    
     def append(self, state, dict_, value, initiator, passive=PASSIVE_OFF):
         self.set(state, dict_, value, initiator, passive=passive)
 
index 81e6829e438b94b908f82cbeb1beaa99d2ba0bc2..1b3e72fc7f13546be28608c3583627c45a938e90 100644 (file)
@@ -2304,6 +2304,37 @@ class DeclarativeMixinTest(DeclarativeTestBase):
         assert Specific.__table__ is General.__table__
         eq_(General.__table__.kwargs, {'mysql_engine': 'InnoDB'})
 
+    def test_columns_single_table_inheritance(self):
+        class MyMixin(object):
+            foo = Column('foo', Integer)
+            bar = Column('bar_newname', Integer)
+            
+        class General(Base, MyMixin):
+            __tablename__ = 'test'
+            id = Column(Integer, primary_key=True)
+            type_ = Column(String(50))
+            __mapper__args = {'polymorphic_on': type_}
+
+        class Specific(General):
+            __mapper_args__ = {'polymorphic_identity': 'specific'}
+
+    @testing.fails_if(lambda: True, "Unhandled declarative use case")
+    def test_columns_joined_table_inheritance(self):
+        class MyMixin(object):
+            foo = Column('foo', Integer)
+            bar = Column('bar_newname', Integer)
+            
+        class General(Base, MyMixin):
+            __tablename__ = 'test'
+            id = Column(Integer, primary_key=True)
+            type_ = Column(String(50))
+            __mapper__args = {'polymorphic_on': type_}
+
+        class Specific(General):
+            __tablename__ = 'sub'
+            id = Column(Integer, ForeignKey('test.id'), primary_key=True)
+            __mapper_args__ = {'polymorphic_identity': 'specific'}
+        
     def test_table_args_overridden(self):
 
         class MyMixin: