Legacy Query API

About the Legacy Query API

This page contains the Python generated documentation for the Query construct, which for many years was the sole SQL interface when working with the SQLAlchemy ORM. As of version 2.0, an all new way of working is now the standard approach, where the same select() construct that works for Core works just as well for the ORM, providing a consistent interface for building queries.

For any application that is built on the SQLAlchemy ORM prior to the 2.0 API, the Query API will usually represents the vast majority of database access code within an application, and as such the majority of the Query API is not being removed from SQLAlchemy. The Query object behind the scenes now translates itself into a 2.0 style select() object when the Query object is executed, so it now is just a very thin adapter API.

For an introduction to writing SQL for ORM objects in the 2.0 style, start with the SQLAlchemy 2.0 Tutorial. Additional reference for 2.0 style querying is at ORM Querying Guide.

The Query Object

Query is produced in terms of a given Session, using the Session.query() method:

q = session.query(SomeMappedClass)

Following is the full interface for the Query object.

ORM-Specific Query Constructs

This section has moved to Additional ORM API Constructs.