Skip to main content
Build Status Code Coverage

This is a tool that reads the structure of an existing database and generates the appropriate SQLAlchemy model code, using the declarative style if possible.

This tool was written as a replacement for sqlautocode, which was suffering from several issues (including, but not limited to, incompatibility with Python 3 and the latest SQLAlchemy version).

Features

  • Supports SQLAlchemy 2.x

  • Produces declarative code that almost looks like it was hand written

  • Produces PEP 8 compliant code

  • Accurately determines relationships, including many-to-many, one-to-one

  • Automatically detects joined table inheritance

  • Excellent test coverage

Installation

To install, do:

pip install sqlacodegen

To include support for the PostgreSQL CITEXT extension type (which should be considered as tested only under a few environments) specify the citext extra:

pip install sqlacodegen[citext]

To include support for the PostgreSQL GEOMETRY, GEOGRAPHY, and RASTER types (which should be considered as tested only under a few environments) specify the geoalchemy2 extra:

To include support for the PostgreSQL PGVECTOR extension type, specify the pgvector extra:

pip install sqlacodegen[pgvector]
pip install sqlacodegen[geoalchemy2]

Quickstart

At the minimum, you have to give sqlacodegen a database URL. The URL is passed directly to SQLAlchemy’s create_engine() method so please refer to SQLAlchemy’s documentation for instructions on how to construct a proper URL.

Examples:

sqlacodegen postgresql:///some_local_db
sqlacodegen --generator tables mysql+pymysql://user:password@localhost/dbname
sqlacodegen --generator dataclasses sqlite:///database.db
# --engine-arg values are parsed with ast.literal_eval
sqlacodegen oracle+oracledb://user:pass@127.0.0.1:1521/XE --engine-arg thick_mode=True
sqlacodegen oracle+oracledb://user:pass@127.0.0.1:1521/XE --engine-arg thick_mode=True --engine-arg connect_args='{"user": "user", "dsn": "..."}'

To see the list of generic options:

sqlacodegen --help

Available generators

The selection of a generator determines the

The following built-in generators are available:

  • tables (only generates Table objects, for those who don’t want to use the ORM)

  • declarative (the default; generates classes inheriting from declarative_base()

  • dataclasses (generates dataclass-based models; v1.4+ only)

  • sqlmodels (generates model classes for SQLModel)

Generator-specific options

The following options can be turned on by passing them using --options (multiple values must be delimited by commas, e.g. --options noconstraints,nobidi):

  • tables

    • noconstraints: ignore constraints (foreign key, unique etc.)

    • nocomments: ignore table/column comments

    • noindexes: ignore indexes

  • declarative

    • all the options from tables

    • use_inflect: use the inflect library when naming classes and relationships (turning plural names into singular; see below for details)

    • nojoined: don’t try to detect joined-class inheritance (see below for details)

    • nobidi: generate relationships in a unidirectional fashion, so only the many-to-one or first side of many-to-many relationships gets a relationship attribute, as on v2.X

  • dataclasses

    • all the options from declarative

  • sqlmodels

    • all the options from declarative

Model class generators

The code generators that generate classes try to generate model classes whenever possible. There are two circumstances in which a Table is generated instead:

  • the table has no primary key constraint (which is required by SQLAlchemy for every model class)

  • the table is an association table between two other tables (see below for the specifics)

Model class naming logic

By default, table names are converted to valid PEP 8 compliant class names by replacing all characters unsuitable for Python identifiers with _. Then, each valid parts (separated by underscores) are title cased and then joined together, eliminating the underscores. So, example_name becomes ExampleName.

If the use_inflect option is used, the table name (which is assumed to be in English) is converted to singular form using the “inflect” library. For example, sales_invoices becomes SalesInvoice. Since table names are not always in English, and the inflection process is far from perfect, inflection is disabled by default.

Relationship detection logic

Relationships are detected based on existing foreign key constraints as follows:

  • many-to-one: a foreign key constraint exists on the table

  • one-to-one: same as many-to-one, but a unique constraint exists on the column(s) involved

  • many-to-many: (not implemented on the sqlmodel generator) an association table is found to exist between two tables

A table is considered an association table if it satisfies all of the following conditions:

  1. has exactly two foreign key constraints

  2. all its columns are involved in said constraints

Relationship naming logic

Relationships are typically named based on the table name of the opposite class. For example, if a class has a relationship to another class with the table named companies, the relationship would be named companies (unless the use_inflect option was enabled, in which case it would be named company in the case of a many-to-one or one-to-one relationship).

A special case for single column many-to-one and one-to-one relationships, however, is if the column is named like employer_id. Then the relationship is named employer due to that _id suffix.

For self referential relationships, the reverse side of the relationship will be named with the _reverse suffix appended to it.

Customizing code generation logic

If the built-in generators with all their options don’t quite do what you want, you can customize the logic by subclassing one of the existing code generator classes. Override whichever methods you need, and then add an entry point in the sqlacodegen.generators namespace that points to your new class. Once the entry point is in place (you typically have to install the project with pip install), you can use --generator <yourentrypoint> to invoke your custom code generator.

For examples, you can look at sqlacodegen’s own entry points in its pyproject.toml.

Getting help

If you have problems or other questions, you should start a discussion on the sqlacodegen discussion forum. As an alternative, you could also try your luck on the sqlalchemy room on Gitter.

Metadata

Release files for sqlacodegen 3.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sqlacodegen 3.1.0
File Size Uploaded Provenance Actions
sqlacodegen-3.1.0.tar.gz 42.5 kB With provenance Details for sqlacodegen-3.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sqlacodegen 3.1.0
File Interpreter ABI Platform Actions
sqlacodegen-3.1.0-py3-none-any.whl Python 3 none any Details for sqlacodegen-3.1.0-py3-none-any.whl

Total release size: 65.2 kB

Release files / sqlacodegen-3.1.0.tar.gz

Download URL sqlacodegen-3.1.0.tar.gz
Size 42.5 kB
Tags Source
SHA-256 checksum
How to use checksums
b4042c7b553e66faf3d9edfdc14d134ee6d06e1b11ed83430d58095993199a62
BLAKE2b-256 checksum
How to use checksums
42e4ae6623e353ffb961124604f27d86666f2a55fb56f6d2d7e2c68a86161af6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2025.

Transparency log

Release files / sqlacodegen-3.1.0-py3-none-any.whl

Download URL sqlacodegen-3.1.0-py3-none-any.whl
Size 22.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
877efdaefd43b143351f9ddbff51fcba89a4d8ce24054873d48830f6797715d3
BLAKE2b-256 checksum
How to use checksums
a18af89b70adfd37ad1fc96619d7c7d7c256e78db65ef7a18985e116b62e7887
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2025.

Transparency log

Security for sqlacodegen 3.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for sqlacodegen-3.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Aug 10, 2025.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

3.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page