Commits

Mike Bayer committed 775b404

Fixed bug whereby ORM would run the wrong kind of
query when refreshing an inheritance-mapped class
where the superclass was mapped to a non-Table
object, like a custom join() or a select(),
running a query that assumed a hierarchy that's
mapped to individual Table-per-class.
[ticket:2697]

Comments (0)

Files changed (3)

doc/build/changelog/changelog_08.rst

 
     .. change::
       :tags: bug, orm
+      :tickets: 2697
+
+      Fixed bug whereby ORM would run the wrong kind of
+      query when refreshing an inheritance-mapped class
+      where the superclass was mapped to a non-Table
+      object, like a custom join() or a select(),
+      running a query that assumed a hierarchy that's
+      mapped to individual Table-per-class.
+
+    .. change::
+      :tags: bug, orm
 
       Fixed `__repr__()` on mapper property constructs
       to work before the object is initialized, so

lib/sqlalchemy/orm/mapper.py

             for mapper in reversed(list(self.iterate_to_root())):
                 if mapper.local_table in tables:
                     start = True
+                elif not isinstance(mapper.local_table, expression.TableClause):
+                    return None
                 if start and not mapper.single:
                     allconds.append(visitors.cloned_traverse(
                                                 mapper.inherit_condition,

test/orm/inheritance/test_basic.py

             Column('b', String(10))
         )
 
+    def test_no_optimize_on_map_to_join(self):
+        base, sub = self.tables.base, self.tables.sub
+
+        class Base(fixtures.ComparableEntity):
+            pass
+
+        class JoinBase(fixtures.ComparableEntity):
+            pass
+        class SubJoinBase(JoinBase):
+            pass
+
+        mapper(Base, base)
+        mapper(JoinBase, base.outerjoin(sub), properties={
+                'id': [base.c.id, sub.c.id],
+                'counter': [base.c.counter, sub.c.counter]
+            })
+        mapper(SubJoinBase, inherits=JoinBase)
+
+        sess = Session()
+        sess.add(Base(data='data'))
+        sess.commit()
+
+        sjb = sess.query(SubJoinBase).one()
+        sjb_id = sjb.id
+        sess.expire(sjb)
+
+        # this should not use the optimized load,
+        # which assumes discrete tables
+        def go():
+            eq_(sjb.data, 'data')
+
+        self.assert_sql_execution(
+            testing.db,
+            go,
+            CompiledSQL(
+                "SELECT base.counter AS base_counter, "
+                "sub.counter AS sub_counter, base.id AS base_id, "
+                "sub.id AS sub_id, base.data AS base_data, "
+                "base.type AS base_type, sub.sub AS sub_sub, "
+                "sub.counter2 AS sub_counter2 FROM base "
+                "LEFT OUTER JOIN sub ON base.id = sub.id "
+                "WHERE base.id = :param_1",
+                {'param_1': sjb_id}
+            ),
+        )
+
+
     def test_optimized_passes(self):
         """"test that the 'optimized load' routine doesn't crash when
         a column in the join condition is not available."""
             pass
         mapper(Base, base, polymorphic_on=base.c.type, polymorphic_identity='base')
         mapper(Sub, sub, inherits=Base, polymorphic_identity='sub', properties={
-            'concat':column_property(sub.c.sub + "|" + sub.c.sub)
+            'concat': column_property(sub.c.sub + "|" + sub.c.sub)
         })
         sess = sessionmaker()()
         s1 = Sub(data='s1data', sub='s1sub')
             pass
         mapper(Base, base, polymorphic_on=base.c.type, polymorphic_identity='base')
         mapper(Sub, sub, inherits=Base, polymorphic_identity='sub', properties={
-            'concat':column_property(base.c.data + "|" + sub.c.sub)
+            'concat': column_property(base.c.data + "|" + sub.c.sub)
         })
         sess = sessionmaker()()
         s1 = Sub(data='s1data', sub='s1sub')