Skip to content

migration note on subqueries #11107

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions doc/build/changelog/migration_20.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2095,6 +2095,38 @@ when using an explicit :func:`_orm.aliased` object, both from a user point
of view as well as how the internals of the SQLAlchemy ORM must handle it.


Filtering operations no longer accept explicit subqueries
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

**Synopsis**

Support for accepting explicit subqueries in filtering operations, such as
`_sql.expression.ColumnOperators.in_` and
`_sql.expression.ColumnOperators.not_in`, has been removed in 2.0 ::

subq = (
session.query(User.id)
.filter(User.name == "foo")
).subquery()
q = (
session.query(User)
.filter(User.id.in_(subq))
)

**Migration to 2.0**

Under 2.0, SQLAlchemy will automatically interpret a fully constructed query
as a subquery based on the implied context ::

subq = (
session.query(User.id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for migration to 2.0 the select() construct, not session.query(), should be used. with select(), you'd want to call subquery() on that unless you are using it inside of in_(). so this note should also mention that this is specific to in_()/not_in()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm going to move stuff around and incorporate your info. I'll use github for a bit and try to avoid gerrit if you don't mind.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I waited a bit before revisiting, and think this is the approach. Ready for review.

.filter(User.name == "foo")
)
q = (
session.query(User)
.filter(User.id.in_(subq))
)

.. _joinedload_not_uniqued:

ORM Rows not uniquified by default
Expand Down
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy