sqlacodegen
Automatic model code generator for SQLAlchemy
What it is and what it does
sqlacodegen is a command-line tool that introspects an existing database and generates the corresponding SQLAlchemy model definitions. It reads the database schema—tables, columns, constraints, relationships—and outputs Python code that you can use directly in your application. The tool supports multiple code generation styles: traditional declarative classes, dataclasses, and SQLModel, letting you choose the pattern that fits your project.
The generator handles relationship detection (many-to-one, one-to-one, many-to-many), joined table inheritance, and produces PEP 8 compliant code. It includes options to control naming (using the inflect library to singularize class names), constraint handling, and dialect-specific type preservation. This is useful when you have an existing database and want to avoid hand-writing boilerplate ORM models, or when you need to keep models synchronized with schema changes.
Use it for:
- Quickly bootstrap SQLAlchemy models from a legacy database without manual class definition.
- Generate dataclass-based models for modern Python projects that prefer immutability or type hints.
- Create SQLModel definitions for FastAPI applications that need both ORM and API schema in one model.
- Reverse-engineer relationship logic (foreign keys, many-to-many joins) automatically instead of writing it by hand.
- Maintain model code when the database schema changes by regenerating and comparing the output.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Reads an existing database schema and generates SQLAlchemy model code automatically, supporting multiple output formats including declarative classes, dataclasses, and SQLModel.
Yes. This is a mature, actively maintained tool (Production/Stable status, recent release) with low install friction and no known vulnerabilities. It solves a real problem—avoiding tedious hand-written ORM boilerplate—and is well-suited for projects with existing databases or frequent schema changes. The permissive MIT license adds no restrictions.
Install
sqlacodegen on PyPI
pip
pip install sqlacodegenuv
uv add sqlacodegenpoetry
poetry add sqlacodegenInstalling sqlacodegen
Before you install
Low friction install with only two runtime dependencies (SQLAlchemy and inflect). Actively maintained with a recent release and 2364 repository stars; supports Python 3.10 through 3.14.
License in practice
MIT license permits commercial and private use with minimal restrictions; suitable for most projects.
Quickstart
pip install sqlacodegen
sqlacodegen postgresql:///some_local_db
sqlacodegen --generator dataclasses sqlite:///database.db
Requires Python 3.10 or later; database URL must be valid for SQLAlchemy's create_engine().
Verify before relying
- Whether the generated code requires manual refinement for complex schemas or edge cases.
- Performance characteristics when processing very large database schemas.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 2 — SQLAlchemy, inflect |
| Maintenance | actively maintained — 56 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 1,842,566/month — #3,495 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: sqlacodegen-4.0.4-py3-none-any.whl
Keywords: sqlalchemy
Tags
More Database packages
psycopg2-binary is a PostgreSQL database…
copyleft · top 1,000 on PyPI
redisPython client library for connecting to and…
permissive · top 1,000 on PyPI
ydbYDB Python SDK is the official client library…
permissive · top 1,000 on PyPI
snowflake-connector-pythonConnects Python applications to Snowflake data…
permissive · top 1,000 on PyPI
sqlparsesqlparse tokenizes SQL text into a tree of…
permissive · top 1,000 on PyPI
dbt-adaptersProvides base adapter protocols and shared…
permissive · top 1,000 on PyPI
sqlalchemy-schemadisplayGenerates entity-relationship and UML diagrams…
permissive · top 15,000 on PyPI
SQLAlchemySQLAlchemy is a Python SQL toolkit and Object…
permissive · top 100 on PyPI
SQLAlchemy-ContinuumSQLAlchemy-Continuum adds automatic versioning…
permissive · top 5,000 on PyPI
paracelsusParacelsus generates Entity Relationship…
permissive · top 15,000 on PyPI
sqlalchemy-reprAdds automatic, human-readable repr() output to…
unclear · top 15,000 on PyPI
app-modelProvides a declarative schema for organizing…
permissive · top 15,000 on PyPI
sqlmodelSQLModel combines SQLAlchemy and Pydantic to…
permissive · top 5,000 on PyPI
graphene-sqlalchemyGraphene-SQLAlchemy exposes SQLAlchemy database…
permissive · top 15,000 on PyPI
eralchemyGenerates Entity Relation (ER) diagrams from…
permissive · top 5,000 on PyPI
access-parserParses Microsoft Access database files (.mdb…
permissive · top 15,000 on PyPI