]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Declarative locates the "inherits" class using a search
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 19 Feb 2009 15:48:37 +0000 (15:48 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 19 Feb 2009 15:48:37 +0000 (15:48 +0000)
through __bases__, to skip over mixins that are local
to subclasses.

CHANGES
lib/sqlalchemy/ext/declarative.py
test/ext/declarative.py

diff --git a/CHANGES b/CHANGES
index f6b178f5cc4da7e7286be0c0715c63a724ec76a4..2d03e71164f8f71adb5fd3f6f40db3cfee6c6348 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -93,10 +93,13 @@ CHANGES
     - Fixed a recursive pickling issue in serializer, triggered
       by an EXISTS or other embedded FROM construct.
 
+    - Declarative locates the "inherits" class using a search
+      through __bases__, to skip over mixins that are local
+      to subclasses.   
+      
     - Declarative figures out joined-table inheritance primary join
       condition even if "inherits" mapper argument is given 
-      explicitly.  Allows mixins to be used with joined table
-      inheritance.
+      explicitly. 
 
     - Declarative will properly interpret the "foreign_keys" argument
       on a backref() if it's a string.
index f1b2e65fb59931ea51ae7030db2d995fc337aa3a..79677c084458fd92b74ff22b7a8ddc33e59bae16 100644 (file)
@@ -400,6 +400,7 @@ from sqlalchemy.schema import Table, Column, MetaData
 from sqlalchemy.orm import synonym as _orm_synonym, mapper, comparable_property, class_mapper
 from sqlalchemy.orm.interfaces import MapperProperty
 from sqlalchemy.orm.properties import PropertyLoader, ColumnProperty
+from sqlalchemy.orm.util import _is_mapped_class
 from sqlalchemy import util, exceptions
 from sqlalchemy.sql import util as sql_util
 
@@ -483,10 +484,10 @@ def _as_declarative(cls, classname, dict_):
             
     mapper_args = getattr(cls, '__mapper_args__', {})
     if 'inherits' not in mapper_args:
-        inherits = cls.__mro__[1]
-        inherits = cls._decl_class_registry.get(inherits.__name__, None)
-        if inherits:
-            mapper_args['inherits'] = inherits
+        for c in cls.__bases__:
+            if _is_mapped_class(c):
+                mapper_args['inherits'] = cls._decl_class_registry.get(c.__name__, None)
+                break
 
     if hasattr(cls, '__mapper_cls__'):
         mapper_cls = util.unbound_method_to_callable(cls.__mapper_cls__)
index fb8190f2edb3cc201b8fc2e48da03284fc2bb35d..81009384b92092f0febd11f5a50c80757c189f3a 100644 (file)
@@ -768,7 +768,7 @@ class DeclarativeInheritanceTest(DeclarativeTestBase):
         
         # compile succeeds because inherit_condition is honored
         compile_mappers()
-
+    
     def test_joined(self):
         class Company(Base, ComparableEntity):
             __tablename__ = 'companies'
@@ -837,6 +837,25 @@ class DeclarativeInheritanceTest(DeclarativeTestBase):
         def go():
             assert sess.query(Person).filter(Manager.name=='dogbert').one().id
         self.assert_sql_count(testing.db, go, 1)
+
+    def test_subclass_mixin(self):
+        class Person(Base, ComparableEntity):
+            __tablename__ = 'people'
+            id = Column('id', Integer, primary_key=True)
+            name = Column('name', String(50))
+            discriminator = Column('type', String(50))
+            __mapper_args__ = {'polymorphic_on':discriminator}
+        
+        class MyMixin(object):
+            pass
+            
+        class Engineer(MyMixin, Person):
+            __tablename__ = 'engineers'
+            __mapper_args__ = {'polymorphic_identity':'engineer'}
+            id = Column('id', Integer, ForeignKey('people.id'), primary_key=True)
+            primary_language = Column('primary_language', String(50))
+            
+        assert class_mapper(Engineer).inherits is class_mapper(Person)
         
     def test_with_undefined_foreignkey(self):
         class Parent(Base):