Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

why does query.statement add no_replacement_traverse #4304

Closed
sqlalchemy-bot opened this issue Jul 14, 2018 · 3 comments
Closed

why does query.statement add no_replacement_traverse #4304

sqlalchemy-bot opened this issue Jul 14, 2018 · 3 comments
Labels
bug Something isn't working orm
Milestone

Comments

@sqlalchemy-bot
Copy link
Collaborator

Migrated issue, originally created by Michael Bayer (@zzzeek)

e.g.

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:

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))

# 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

@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

running it through at https://gerrit.sqlalchemy.org/#/c/zzzeek/sqlalchemy/+/823/ to see if this impacts anything

@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

Don't apply no-traverse to query.statement

Fixed long-standing issue in :class:.Query where a scalar subquery such
as produced by :meth:.Query.exists, :meth:.Query.as_scalar and other
derivations from :attr:.Query.statement would not correctly be adapted
when used in a new :class:.Query that required entity adaptation, such as
when the query were turned into a union, or a from_self(), etc. The change
removes the "no adaptation" annotation from the :func:.select object
produced by the :attr:.Query.statement accessor.

Change-Id: I554e0e909ac6ee785ec3b3b14aaec9d235aa28cf
Fixes: #4304

fb37722

@sqlalchemy-bot
Copy link
Collaborator Author

Changes by Michael Bayer (@zzzeek):

  • changed status to closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working orm
Projects
None yet
Development

No branches or pull requests

1 participant