From: Chris Withers Date: Tue, 13 Apr 2010 10:29:39 +0000 (+0100) Subject: helper method for spotting inherited tables X-Git-Tag: rel_0_6_0~23^2~6 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=aaa0ba0525da3186e3ad88194c66f667685c5a67;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git helper method for spotting inherited tables --- diff --git a/lib/sqlalchemy/ext/declarative.py b/lib/sqlalchemy/ext/declarative.py index 0c7aa68db0..8ed83d6cd9 100755 --- a/lib/sqlalchemy/ext/declarative.py +++ b/lib/sqlalchemy/ext/declarative.py @@ -528,7 +528,16 @@ def instrument_declarative(cls, registry, metadata): cls._decl_class_registry = registry cls.metadata = metadata _as_declarative(cls, cls.__name__, cls.__dict__) - + +def has_inherited_table(cls): + """Given a class, return True if any of the classes it inherits from has a mapped + table, otherwise return False. + """ + for class_ in cls.__mro__: + if getattr(class_,'__table__',None) is not None: + return True + return False + def _as_declarative(cls, classname, dict_): # dict_ will be a dictproxy, which we can't write to, and we need to! diff --git a/test/ext/test_declarative.py b/test/ext/test_declarative.py index 0c7aef6dc8..19e4cdb8a5 100644 --- a/test/ext/test_declarative.py +++ b/test/ext/test_declarative.py @@ -2289,14 +2289,13 @@ class DeclarativeMixinTest(DeclarativeTestBase): eq_(Joined.__table__.c.keys(),['id','timestamp']) eq_(Joined.__table__.kwargs,{'mysql_engine': 'InnoDB'}) - def test_tablename_propagation(self): + def test_has_inherited_table(self): class NoJoinedTableNameMixin: @classproperty def __tablename__(cls): - for class_ in cls.__mro__: - if getattr(class_,'__table__',None) is not None: - return None + if decl.has_inherited_table(cls): + return None return cls.__name__.lower() class BaseType(Base, NoJoinedTableNameMixin):