From c5579f77e26e1449f82888c4a95466a77642130d Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Thu, 21 Oct 2010 16:54:39 -0400 Subject: [PATCH] - A mixin can now specify a column that overrides a column of the same name associated with a superclass. Thanks to Oystein Haaland. --- CHANGES | 6 +++++- lib/sqlalchemy/ext/declarative.py | 2 +- test/ext/test_declarative.py | 21 +++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CHANGES b/CHANGES index 9eb89b07bf..97a3cceb9b 100644 --- a/CHANGES +++ b/CHANGES @@ -192,7 +192,11 @@ CHANGES inheritance scheme where the attribute name is different than that of the column. [ticket:1930], [ticket:1931]. - + + - A mixin can now specify a column that overrides + a column of the same name associated with a superclass. + Thanks to Oystein Haaland. + - engine - Fixed a regression in 0.6.4 whereby the change that diff --git a/lib/sqlalchemy/ext/declarative.py b/lib/sqlalchemy/ext/declarative.py index debfdfe571..dd2df63d30 100755 --- a/lib/sqlalchemy/ext/declarative.py +++ b/lib/sqlalchemy/ext/declarative.py @@ -1015,7 +1015,7 @@ def _as_declarative(cls, classname, dict_): if name not in dict_ and not ( '__table__' in dict_ and (obj.name or name) in dict_['__table__'].c - ): + ) and name not in potential_columns: potential_columns[name] = \ column_copies[obj] = \ obj.copy() diff --git a/test/ext/test_declarative.py b/test/ext/test_declarative.py index 0a6b17e27a..72e2edf30e 100644 --- a/test/ext/test_declarative.py +++ b/test/ext/test_declarative.py @@ -2170,7 +2170,28 @@ class DeclarativeMixinTest(DeclarativeTestBase): eq_(obj.name, 'testing') eq_(obj.foo(), 'bar1') eq_(obj.baz, 'fu') + + def test_mixin_overrides(self): + """test a mixin that overrides a column on a superclass.""" + + class MixinA(object): + foo = Column(String(50)) + + class MixinB(MixinA): + foo = Column(Integer) + class MyModelA(Base, MixinA): + __tablename__ = 'testa' + id = Column(Integer, primary_key=True) + + class MyModelB(Base, MixinB): + __tablename__ = 'testb' + id = Column(Integer, primary_key=True) + + eq_(MyModelA.__table__.c.foo.type.__class__, String) + eq_(MyModelB.__table__.c.foo.type.__class__, Integer) + + def test_not_allowed(self): class MyMixin: -- 2.47.2