diff options
-rw-r--r-- | CHANGES | 3 | ||||
-rw-r--r-- | lib/sqlalchemy/orm/mapper.py | 3 | ||||
-rw-r--r-- | test/orm/inheritance/test_basic.py | 22 |
3 files changed, 22 insertions, 6 deletions
@@ -195,7 +195,8 @@ CHANGES would have no 'polymorphic_on' column if it didn't also specify a 'polymorphic_identity', leading to strange errors upon refresh, wrong class loaded when querying - from that target. [ticket:2038] + from that target. Also emits the correct WHERE criterion + when using single table inheritance. [ticket:2038] - sql - Column.copy(), as used in table.tometadata(), copies the diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py index 63d2c4de9..db445076c 100644 --- a/lib/sqlalchemy/orm/mapper.py +++ b/lib/sqlalchemy/orm/mapper.py @@ -983,8 +983,7 @@ class Mapper(object): def _single_table_criterion(self): if self.single and \ self.inherits and \ - self.polymorphic_on is not None and \ - self.polymorphic_identity is not None: + self.polymorphic_on is not None: return self.polymorphic_on.in_( m.polymorphic_identity for m in self.self_and_descendants) diff --git a/test/orm/inheritance/test_basic.py b/test/orm/inheritance/test_basic.py index 55cc43993..96c31d516 100644 --- a/test/orm/inheritance/test_basic.py +++ b/test/orm/inheritance/test_basic.py @@ -1520,25 +1520,32 @@ class NoPolyIdentInMiddleTest(_base.MappedTest): @classmethod def define_tables(cls, metadata): Table('base', metadata, - Column('id', Integer, primary_key=True, test_needs_autoincrement=True), + Column('id', Integer, primary_key=True, + test_needs_autoincrement=True), Column('type', String(50), nullable=False), ) @classmethod def setup_classes(cls): - class A(_base.BasicEntity): + class A(_base.ComparableEntity): pass class B(A): pass class C(B): pass + class D(B): + pass + class E(A): + pass @classmethod @testing.resolve_artifact_names def setup_mappers(cls): mapper(A, base, polymorphic_on=base.c.type) - mapper(B, inherits=A) + mapper(B, inherits=A, ) mapper(C, inherits=B, polymorphic_identity='c') + mapper(D, inherits=B, polymorphic_identity='d') + mapper(E, inherits=A, polymorphic_identity='e') @testing.resolve_artifact_names def test_load_from_middle(self): @@ -1561,6 +1568,15 @@ class NoPolyIdentInMiddleTest(_base.MappedTest): assert class_mapper(B).polymorphic_on is base.c.type assert class_mapper(C).polymorphic_on is base.c.type + @testing.resolve_artifact_names + def test_load_multiple_from_middle(self): + s = Session() + s.add_all([C(), D(), E()]) + eq_( + s.query(B).order_by(base.c.type).all(), + [C(), D()] + ) + class DeleteOrphanTest(_base.MappedTest): """Test the fairly obvious, that an error is raised when attempting to insert an orphan. |