ORM 查询指南

ORM Querying Guide

本节概述了使用 SQLAlchemy ORM 发出查询的 2.0 风格 (2.0 style) 使用方法。

阅读本节的读者应熟悉 SQLAlchemy 统一教程 中的 SQLAlchemy 概述,特别是这里的大部分内容扩展了 使用 SELECT 语句 中的内容。

对于 SQLAlchemy 1.x 用户

在 SQLAlchemy 2.x 系列中,ORM 的 SQL SELECT 语句使用与 Core 中相同的 select() 构造来构建,然后使用 SessionSession.execute() 方法调用(正如现在用于 启用 ORM 的 INSERT、UPDATE 和 DELETE 语句 功能的 update()delete() 构造)。然而,遗留的 Query 对象继续作为新系统上的一个薄外观可用,以支持基于 1.x 系列构建的应用程序,无需全面替换所有查询。有关此对象的参考,请参阅 旧式查询 API 部分。

This section provides an overview of emitting queries with the SQLAlchemy ORM using 2.0 style usage.

Readers of this section should be familiar with the SQLAlchemy overview at SQLAlchemy 统一教程, and in particular most of the content here expands upon the content at 使用 SELECT 语句.

For users of SQLAlchemy 1.x

In the SQLAlchemy 2.x series, SQL SELECT statements for the ORM are constructed using the same select() construct as is used in Core, which is then invoked in terms of a Session using the Session.execute() method (as are the update() and delete() constructs now used for the 启用 ORM 的 INSERT、UPDATE 和 DELETE 语句 feature). However, the legacy Query object, which performs these same steps as more of an “all-in-one” object, continues to remain available as a thin facade over this new system, to support applications that were built on the 1.x series without the need for wholesale replacement of all queries. For reference on this object, see the section 旧式查询 API.