API Reference

SQLAlchemy

Python SQL toolkit and ORM with a core expression language and a declarative ORM layer.

Official Documentation ↗
CompatibilityThis documentation covers version v2.0. Check the official docs for the latest API surface.

Installation

pippip install sqlalchemy

Methods

SQLAlchemy is the Python SQL toolkit and Object Relational Mapper. It provides a full suite of well known enterprise-level persistence patterns, designed for efficient and high-performing database access.

Key Features

  • Core: SQL expression language for direct queries
  • ORM: Declarative models with relationships and sessions
  • Engines: Connection pooling and dialect support
  • Migrations: Pairs with Alembic for schema evolution
  • Type hints: Full typing support in 2.0

Errors

The official API reference declares the following error types:

API reference evidence

Fetched from official sources on 2026-11-08T17:41:29+08:00 via PowerShell Invoke-WebRequest 下载官方文档页 docs.sqlalchemy.org/en/20/{core/engines,core/selectable,orm/session_api,orm/mapping_api,core/exceptions}.html 并去除 HTML 标签,正则提取签名与异常声明;WebFetch https://docs.sqlalchemy.org/en/20/core/engines.html 交叉核对.

FAQ

What is the difference between Core and ORM?
Core is a SQL expression language for constructing and executing SQL directly with tables and columns. The ORM builds on Core with mapped classes, identity maps, and unit-of-work sessions. Use Core for performance, ORM for convenience.
What is the unit of work pattern in SQLAlchemy?
The Session tracks changes to objects and flushes them as a batch of SQL statements at commit time. This groups writes, maintains identity, and lets you work with objects instead of raw SQL.
Should I use 1.x or 2.x style?
SQLAlchemy 2.0 unifies the API with a typed select() style and is recommended for new code. Legacy 1.x Query API still works but is deprecated. The 2.0 style is more explicit and type-checker friendly.