Session.query() removal in SQLAlchemy 2.0 migration
21K reputation · 06 Aug 2022, 07:35 UTC
Transitioning from Legacy Query API to select()
SQLAlchemy 2.0 introduces a significant architectural shift by removing the legacy Session.query() method in favor of the Core-style select() construct. While the 1.4 series provided a compatibility shim via the future=True flag on create_engine, this transition boundary is absolute in version 2.0.
The primary goal is to migrate existing ORM codebases to the new session.execute(select(Model)) pattern without introducing regressions in result handling or auto-flush behavior. There is uncertainty regarding how to handle complex legacy queries that rely on orm.Query specific methods when moving to the scalars() execution pattern.
- What is the recommended approach for replacing
Query.filter_by()andQuery.order_by()chains within the newselect()syntax? - How does the removal of the
queryattribute affect third-party extensions that have not yet updated to the 2.0 API?