[Sqlalchemy-tickets] Issue #4304: why does query.statement add no_replacement_traverse (zzzeek/sqla
Brought to you by:
zzzeek
From: Michael B. <iss...@bi...> - 2018-07-14 16:16:25
|
New issue 4304: why does query.statement add no_replacement_traverse https://bitbucket.org/zzzeek/sqlalchemy/issues/4304/why-does-querystatement-add Michael Bayer: e.g. ``` #!diff diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py index 272fed3e23..c623f10a95 100644 --- a/lib/sqlalchemy/orm/query.py +++ b/lib/sqlalchemy/orm/query.py @@ -514,7 +514,7 @@ class Query(object): # TODO: there's no tests covering effects of # the annotation not being there - return stmt._annotate({'no_replacement_traverse': True}) + return stmt def subquery(self, name=None, with_labels=False, reduce_columns=False): """return the full SELECT statement represented by ``` here is a confusing effect of it being there: ``` #!python from sqlalchemy import * from sqlalchemy.orm import * from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() class MyTable(Base): __tablename__ = 'my_table' score = Column(Integer, primary_key=True) my_alias = aliased(MyTable, name="my_alias") s = Session() query_2 = s.query(MyTable).union(s.query(MyTable)) query_2 = query_2.filter(MyTable.score > 5) stmt1 = exists().where(my_alias.score > MyTable.score) stmt2 = s.query(my_alias).filter(my_alias.score > MyTable.score).exists() print(query_2.add_column(stmt1)) print(query_2.add_column(stmt2)) ``` ``` #!sql # correct: SELECT anon_1.my_table_score AS anon_1_my_table_score, EXISTS (SELECT * FROM my_table AS my_alias WHERE my_alias.score > anon_1.my_table_score) AS anon_2 FROM (SELECT my_table.score AS my_table_score FROM my_table UNION SELECT my_table.score AS my_table_score FROM my_table) AS anon_1 WHERE anon_1.my_table_score > :score_1 # incorrect: SELECT anon_1.my_table_score AS anon_1_my_table_score, EXISTS (SELECT 1 FROM my_table AS my_alias, my_table WHERE my_alias.score > my_table.score) AS anon_2 FROM (SELECT my_table.score AS my_table_score FROM my_table UNION SELECT my_table.score AS my_table_score FROM my_table) AS anon_1 WHERE anon_1.my_table_score > :score_1 ``` |