]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
helper method for spotting inherited tables
authorChris Withers <chris@simplistix.co.uk>
Tue, 13 Apr 2010 10:29:39 +0000 (11:29 +0100)
committerChris Withers <chris@simplistix.co.uk>
Tue, 13 Apr 2010 10:29:39 +0000 (11:29 +0100)
lib/sqlalchemy/ext/declarative.py
test/ext/test_declarative.py

index 0c7aa68db02cb3247bd071a5fd07ae960ea4e047..8ed83d6cd947039639524cb82cfb63bbf2e1e8f9 100755 (executable)
@@ -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!
index 0c7aef6dc8589bf24a3e3b46f64b4f8c0c83d9dd..19e4cdb8a50b89f3dba4ff2aa3e7aed0450dcae5 100644 (file)
@@ -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):