I encountered a little strangeness when joining to a class using single table inheritance. I was wondering why I got no results for one particular query. This was originally encountered with PostgreSQL but was successfully reproduced with SQLite. Is this a bug or a user error? ----------------SNIP--------------------SNIP--------------------
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.engine import create_engine from sqlalchemy.schema import Column, ForeignKey from sqlalchemy.types import Integer, Unicode from sqlalchemy.orm.session import Session from sqlalchemy.orm import relationship Base = declarative_base(create_engine('sqlite:///')) class A(Base): __tablename__ = 'a' id = Column(Integer, primary_key=True) disc = Column(Unicode, nullable=False) name = Column(Unicode) __mapper_args__ = {'polymorphic_on': disc} class B(A): __mapper_args__ = {'polymorphic_identity': 'b'} class Z(Base): __tablename__ = 'z' id = Column(Integer, primary_key=True) b_id = Column(Integer, ForeignKey(B.id)) b = relationship(B) Base.metadata.create_all() session = Session() query = session.query(Z, A.name).outerjoin(Z.b).filter(Z.id == 1) print query #SELECT z.id AS z_id, z.b_id AS z_b_id, a.name AS a_name #FROM z LEFT OUTER JOIN a ON a.id = z.b_id AND a.disc IN (?) #WHERE z.id = ? AND a.disc IN (?) # ^- why is this condition here? # # WORKAROUND: # query = session.query(Z, A.name).outerjoin(Z.b).filter(Z.id == 1) # ^- use the superclass instead # #SELECT z.id AS z_id, z.b_id AS z_b_id, a.name AS a_name #FROM z LEFT OUTER JOIN a ON a.id = z.b_id AND a.disc IN (?) #WHERE z.id = ? # ^- no extra WHERE condition this time around -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/RB_1UbBZRogJ. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.