[Sqlalchemy-tickets] Issue #3222: join to single-inh subclass without using a property-based join f
Brought to you by:
zzzeek
|
From: Mike B. <iss...@bi...> - 2014-10-09 23:55:28
|
New issue 3222: join to single-inh subclass without using a property-based join fails to apply single inh criterion https://bitbucket.org/zzzeek/sqlalchemy/issue/3222/join-to-single-inh-subclass-without-using Mike Bayer: ``` #!python from sqlalchemy import Integer, ForeignKey, Column, String from sqlalchemy.orm import Session, relationship from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class A(Base): __tablename__ = 'a' id = Column(Integer, primary_key=True) type = Column(String) cid = Column(ForeignKey('c.id')) c = relationship("C", backref="a") __mapper_args__ = {'polymorphic_on': type} class B(A): __mapper_args__ = {'polymorphic_identity': 'b'} class C(Base): __tablename__ = 'c' id = Column(Integer, primary_key=True) s = Session() print s.query(C, B).filter(C.id == B.cid) print s.query(C, B).join(B, C.id == B.cid) print s.query(C, B).join(B, C.a) ``` first query is OK: ``` #!sql SELECT c.id AS c_id, a.id AS a_id, a.type AS a_type, a.cid AS a_cid FROM c, a WHERE c.id = a.cid AND a.type IN (:type_1) ``` second query lacks the discriminator: ``` #!sql SELECT c.id AS c_id, a.id AS a_id, a.type AS a_type, a.cid AS a_cid FROM c JOIN a ON c.id = a.cid ``` third one is OK: ``` #!sql SELECT c.id AS c_id, a.id AS a_id, a.type AS a_type, a.cid AS a_cid FROM c JOIN a ON c.id = a.cid AND a.type IN (:type_1) ``` the issue is the check inside of _join_entities assumes the ON clause was rendered orm.join() which also adds the criteria in. So this patch resolves: ``` #!diff diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 7b2ea79..0c3bc26 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -1979,7 +1979,7 @@ class Query(object): info.selectable, \ getattr(info, 'is_aliased_class', False) - if right_mapper: + if right_mapper and prop is not None: self._join_entities += (info, ) if right_mapper and prop and \ ``` |