Skip to main content

Automatic model code generator for SQLAlchemy

Project description

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:

  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 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:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sqlacodegen-2.1.0.tar.gz (21.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sqlacodegen-2.1.0-py2.py3-none-any.whl (12.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file sqlacodegen-2.1.0.tar.gz.

File metadata

  • Download URL: sqlacodegen-2.1.0.tar.gz
  • Upload date:
  • Size: 21.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.7

File hashes

Hashes for sqlacodegen-2.1.0.tar.gz
Algorithm Hash digest
SHA256 eae258e36c3dce8dd9543d35a234552d4e37ab5561072b3fb08543c7e881107c
MD5 53a0d5c5a6ab72bbddfdd8526b5bebdb
BLAKE2b-256 75e1cf19321bc2d0b5c5bb77cff3da366efb571f1a02c41630c9859608cad2ec

See more details on using hashes here.

File details

Details for the file sqlacodegen-2.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: sqlacodegen-2.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.7

File hashes

Hashes for sqlacodegen-2.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 69daffff1524f51dde946706289c608a2e7f0a5171d0050945a5eec29dbbaca1
MD5 5048894ee91824bd83f9a2f6cbc004ac
BLAKE2b-256 37e93af8a387ad640fa92e9916f5e0e9fa4c603b18de77e4f80671d1359159b4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page