skillfed

sqlacodegen

Automatic model code generator for SQLAlchemy

sqlacodegen v4.0.4 1.8M downloads/30d#3,495 on PyPI2,364
Permissive license MIT Active released

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 sqlacodegen

uv

uv add sqlacodegen

poetry

poetry add sqlacodegen

Installing 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

Development Status :: 5 - Production/StableEnvironment :: ConsoleIntended Audience :: DevelopersProgramming Language :: PythonProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Topic :: DatabaseTopic :: Software Development :: Code Generators

Tags

sqlalchemy model code generatordatabase schema to python modelsauto-generate orm classes from databasesqlacodegen alternativedeclarative sqlalchemy from existing dbreverse engineer database to sqlalchemy
code-generationorm-toolingschema-reverse-engineering

More Database packages