From: Mike Bayer Date: Thu, 3 Feb 2011 00:03:20 +0000 (-0500) Subject: - Also emits the correct WHERE criterion X-Git-Tag: rel_0_7b1~38 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4f1274fc1fc675e2a482b68d658b36597f243c31;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - Also emits the correct WHERE criterion when using single table inheritance. [ticket:2038] --- diff --git a/CHANGES b/CHANGES index 7eca04fa82..7f5ef2f6fd 100644 --- a/CHANGES +++ b/CHANGES @@ -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 63d2c4de94..db445076c8 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 55cc439935..96c31d516f 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.