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.6.x - 0.9.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

or, failing that:

easy_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

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.

Source code

The source can be browsed at Bitbucket.

Reporting bugs

A bug tracker is provided by bitbucket.org.

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-1.1.4.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

sqlacodegen-1.1.4-py2.py3-none-any.whl (13.1 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

  • Download URL: sqlacodegen-1.1.4.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for sqlacodegen-1.1.4.tar.gz
Algorithm Hash digest
SHA256 3dcde72573b5e0a15ee3aa01a550c1e554d1909b2a895a1e038253e8dbed38e4
MD5 cbc631539fe04024c2debc2d0c6feb7b
BLAKE2b-256 e93532997097e108c5268b3fccfd133853d459e704c57cdf113caa75159bb6a5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for sqlacodegen-1.1.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 8009503945f4fdbb922247076eea5dde491c35f2e64dc6a25433ce51aa5e5f16
MD5 8bfc4f4ed55c25314074cdc542a1d256
BLAKE2b-256 fccc4b15e36d02b125d12f6ae439e64fbcbaebde29326bd6df87da2083c75017

See more details on using hashes here.

Supported by

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