SQLAlchemy
Python SQL toolkit and ORM with a core expression language and a declarative ORM layer.
Official Documentation ↗v2.0. Check the official docs for the latest API surface.Installation
pip install sqlalchemyMethods
No methods match this filter.
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:
SQLAlchemyError(Core Exceptions 基类(The base exception class is SQLAlchemyError)) — official text: “The base exception class is SQLAlchemyError . Exceptions which are raised as a result of DBAPI exceptions are all subclasses of DBAPIError .” (source: https://docs.sqlalchemy.org/en/20/core/exceptions.html#sqlalchemy.exc.SQLAlchemyError, fetched 2026-11-08T17:41:29+08:00)IntegrityError(exception sqlalchemy.exc.IntegrityError(inherits from DatabaseError,Wraps a DB-API IntegrityError)) — official text: “exception sqlalchemy.exc. IntegrityError ¶ inherits from sqlalchemy.exc.DatabaseError Wraps a DB-API IntegrityError.” (source: https://docs.sqlalchemy.org/en/20/core/exceptions.html#sqlalchemy.exc.IntegrityError, fetched 2026-11-08T17:41:29+08:00)NoResultFound(exception sqlalchemy.exc.NoResultFound(inherits from InvalidRequestError,结果必需但未找到)) — official text: “exception sqlalchemy.exc. NoResultFound ¶ inherits from sqlalchemy.exc.InvalidRequestError A database result was required but none was found.” (source: https://docs.sqlalchemy.org/en/20/core/exceptions.html#sqlalchemy.exc.NoResultFound, fetched 2026-11-08T17:41:29+08:00)
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 交叉核对.
create_engine→ signature from https://docs.sqlalchemy.org/en/20/core/engines.html#sqlalchemy.create_engine; official text: “function sqlalchemy. create_engine ( url : str | _url.URL , ** kwargs : Any ) → Engine ¶ Create a new Engine instance.”Declarative Base→ signature from https://docs.sqlalchemy.org/en/20/orm/mapping_api.html#sqlalchemy.orm.DeclarativeBase; official text: “class sqlalchemy.orm. DeclarativeBase ¶ inherits from sqlalchemy.inspection.Inspectable Base class used for declarative class definitions.”Session→ signature from https://docs.sqlalchemy.org/en/20/orm/session_api.html#sqlalchemy.orm.sessionmaker.__init__; official text: “sessionmaker. init ( bind : SessionBind | None = None , * , class : Type [ _S ] = <class ‘sqlalchemy.orm.session.Session’> , autoflush : bool = True , expire_on_commit : bool = True , info : _InfoType | None “select→ signature from https://docs.sqlalchemy.org/en/20/core/selectable.html#sqlalchemy.sql.expression.select; official text: “function sqlalchemy.sql.expression. select ( * entities : _ColumnsClauseArgument [ Any ] , ** __kw : Any ) → Select [ Any ] ¶ Construct a new Select .”
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.
Report a problem with this API docOpens a pre-filled issue on GitHub.