[Sqlalchemy-tickets] Issue #3582: Possible bug with polymorphic entities and `exists` (zzzeek/sqlal
Brought to you by:
zzzeek
|
From: Yegor R. <iss...@bi...> - 2015-11-11 13:24:45
|
New issue 3582: Possible bug with polymorphic entities and `exists` https://bitbucket.org/zzzeek/sqlalchemy/issues/3582/possible-bug-with-polymorphic-entities-and Yegor Roganov: Given class hierarchy from http://docs.sqlalchemy.org/en/rel_1_0/changelog/migration_10.html ```python class Widget(Base): __tablename__ = 'widget' id = Column(Integer, primary_key=True) type = Column(String) related_id = Column(ForeignKey('related.id')) related = relationship("Related", backref="widget") __mapper_args__ = {'polymorphic_on': type} class FooWidget(Widget): __mapper_args__ = {'polymorphic_identity': 'foo'} class Related(Base): __tablename__ = 'related' id = Column(Integer, primary_key=True) ``` query `session.query(exists().where(and_(FooWidget.related_id == 5, FooWidget.id == 6)))` produces strange SQL (`widget.type IN (:type_1)` should be in the subquery): ```sql SELECT EXISTS (SELECT * FROM widget WHERE widget.related_id = :related_id_1 AND widget.id = :id_1) AS anon_1 FROM widget WHERE widget.type IN (:type_1) ``` Query `session.query(session.query(FooWidget).filter(and_(FooWidget.related_id == 5, FooWidget.id == 6)).exists())` is also suboptimal: ```sql SELECT EXISTS (SELECT 1 FROM widget WHERE widget.related_id = :related_id_1 AND widget.id = :id_1 AND widget.type IN (:type_1)) AS anon_1 FROM widget WHERE widget.type IN (:type_1) ``` (note the repeating `widget.type IN (:type_1)` clause) Not sure if this is a bug or I'm just using it wrong, but I figured I'd create an issue. Sorry for the hassle if it's not a bug. |