Alembic
Database migration tool for SQLAlchemy that versions schema changes with upgrade and downgrade scripts.
Official Documentation ↗alembic 1.13+ / SQLAlchemy 2.x / Python 3.9+. Check the official docs for the latest API surface.Installation
pip install alembicMethods
No methods match this filter.
Overview
Alembic versions database schema changes as ordered migration scripts. It integrates with SQLAlchemy metadata, supports autogeneration, and applies or reverts revisions in a controlled order.
Core Methods
init scaffolds the environment, revision creates a script, upgrade applies revisions and downgrade reverts them. Heads, bases and revision identifiers track the current version.
Usage Notes
Keep migrations in version control, review autogenerated diffs, and test both upgrade and downgrade on a copy of production data before deploying.
Common Pitfalls
Autogeneration misses renames and complex changes, so it can drop and recreate columns. Editing an already applied migration in production desynchronizes history. Concurrent branches create multiple heads that must be merged.
Errors
The official API reference declares the following error types:
CommandError(alembic runtime 文档声明:脚本目录存在多个 heads 时 raise CommandError) — official text: “If the script directory has multiple heads, this method raises a CommandError; EnvironmentContext.get_head_revisions() should be preferred.” (source: https://alembic.sqlalchemy.org/en/latest/api/runtime.html, fetched 2026-11-08T09:38:08.940Z)
API reference evidence
Fetched from official sources on 2026-11-08T09:38:08.940Z via WebFetch https://alembic.sqlalchemy.org/en/latest/api/commands.html.
alembic init→ signature from https://alembic.sqlalchemy.org/en/latest/api/commands.html; official text: “alembic.command.init(config:Config, directory: str, template:str = ‘generic’, package: bool =False) → None Initialize a new scripts directory. - directory – string path of the target directory. - template”alembic revision→ signature from https://alembic.sqlalchemy.org/en/latest/api/commands.html; official text: “alembic.command.revision(config:Config, message: str | None = None, autogenerate: bool = False, sql:bool = False, head: str = ‘head’, splice: bool = False, branch_label:_RevIdType | None = None, *version_pa”alembic upgrade→ signature from https://alembic.sqlalchemy.org/en/latest/api/commands.html; official text: “alembic.command.upgrade(config:Config, revision: str, sql: bool =False, tag: str | None = None) →None Upgrade to a later version. - revision – string revision target or range for –sql mode. May be"heads"t”
FAQ
- What does --autogenerate detect?
- It compares model metadata with the database and writes a diff, but it does not catch everything, so review the generated script and add hand-written operations for data migrations.
- How do I handle data migrations?
- Add explicit operations with op.execute or bulk insert/update helpers inside the revision, keeping schema and data changes in the same ordered script.
- Why is downgrade important?
- A working downgrade lets you roll back a failed deploy. Some irreversible changes cannot be downgraded automatically, so implement the reverse operation or document the limitation.
Report a problem with this API docOpens a pre-filled issue on GitHub.