--- id: sqlacodegen version: "4.0.4" license: MIT license_treatment: permissive maintenance: active --- # sqlacodegen — Automatic model code generator for SQLAlchemy License: permissive · Maintenance: active · Downloads: 1.8M/mo ## 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 above — 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 pip install sqlacodegen uv add sqlacodegen 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_current - Install friction: low - Maintenance: active - Downloads: 1.8M/month (top 5,000 on PyPI) - Known vulnerabilities: none known ## Tags sqlalchemy model code generator, database schema to python models, auto-generate orm classes from database, sqlacodegen alternative, declarative sqlalchemy from existing db, reverse engineer database to sqlalchemy, code-generation, orm-tooling, schema-reverse-engineering [View on SkillFed](https://skillfed.io/packages/sqlacodegen) · [View on PyPI](https://pypi.org/project/sqlacodegen/)