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 0.8.x - 1.3.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
Usage instructions
Installation
To install, do:
pip install sqlacodegen
Example usage
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 mysql+oursql://user:password@localhost/dbname sqlacodegen sqlite:///database.db
To see the full list of options:
sqlacodegen --help
Why does it sometimes generate classes and sometimes Tables?
Unless the --noclasses option is used, sqlacodegen tries to generate declarative model classes from each table. 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
The table name (which is assumed to be in English) is converted to singular form using the “inflect” library. Then, every underscore is removed while transforming the next letter to upper case. For example, sales_invoices becomes SalesInvoice.
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: 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:
has exactly two foreign key constraints
all its columns are involved in said constraints
Relationship naming logic
Relationships are typically named based on the opposite class name. For example, if an Employee class has a column named employer which has a foreign key to Company.id, the relationship is named company.
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.
If more than one relationship would be created with the same name, the latter ones are appended numeric suffixes, starting from 1.
Getting help
If you have problems or other questions, you can either:
Ask on the SQLAlchemy Google group, or
Ask on the #sqlalchemy channel on Freenode IRC
Release files for sqlacodegen 2.3.0.post1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| sqlacodegen-2.3.0.post1.tar.gz | 21.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| sqlacodegen-2.3.0.post1-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 34.2 kB
Release files / sqlacodegen-2.3.0.post1.tar.gz
| Download URL | sqlacodegen-2.3.0.post1.tar.gz |
|---|---|
| Size | 21.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
daf34a2557fad3dad8cc4f6d625b66ac4671cb7e9ec8319eb958b47314872a99
|
|
BLAKE2b-256 checksum How to use checksums |
dda114365d345df9d8abd898f20dc6a84df2db0fb9b03b79576e0dd678716522
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.7.16
|
Release files / sqlacodegen-2.3.0.post1-py2.py3-none-any.whl
| Download URL | sqlacodegen-2.3.0.post1-py2.py3-none-any.whl |
|---|---|
| Size | 13.2 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
674d58f54559912c2f785276b50a2f9c52ec7ec9d9aea50a118a002392cb17c3
|
|
BLAKE2b-256 checksum How to use checksums |
134389ee0124f76d4e523c22eef66f3c5e6d50f3513c31144f08df7b9237af8b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.7.16
|