Creates each SQLAlchemy class file separately
Project description
This tool has been forked from at GitHub Python 3 and the latest SQLAlchemy version).
This is a tool that reads the structure of an existing database and generates
the appropriate SQLAlchemy model classes, using the declarative style if
possible.
Features
- Supports SQLAlchemy
- 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
- It creates each SQLAlchemy class file separately under the
"models"
folder.
Usage instructions
Installation
To install, do:
pip install husrev
or, failing that:
easy_install husrev
Example usage
At the minimum, you have to give husrev a database URL.
The URL is passed directly to SQLAlchemy's
create_engine() method so please refer to
SQLAlchemy's documentation http://docs.sqlalchemy.org/en/latest/core/engines.html
for instructions on how to construct a proper URL.
Examples:
husrev postgresql:///some_local_db --noinflect
husrev mysql+oursql://user:password@localhost/dbname
husrev sqlite:///database.db
To see the full list of options:
husrev --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
.
Use --noinflect
disable inflect transform.
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.
Source code
The source can be browsed at GitHub
Reporting bugs
A bug tracker is provided by GitHub.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file husrev-1.5.3.tar.gz
.
File metadata
- Download URL: husrev-1.5.3.tar.gz
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 40bcbe466a11ea9a8b34d14b8e1e1158f92068961716843abc23b4790c715ed2 |
|
MD5 | 961f06f21de3616efd595b980d0191fa |
|
BLAKE2b-256 | e2e84d945dbf43157c771a450098557b75e0a5e7127bfe1da8673930b1193454 |
File details
Details for the file husrev-1.5.3-py3-none-any.whl
.
File metadata
- Download URL: husrev-1.5.3-py3-none-any.whl
- Upload date:
- Size: 11.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.10.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 80e86d828262519ce9bb632c7d544217d0e2139d3dd779debe9f90e44023bbae |
|
MD5 | 45ee1af0c6a0147649fdf8658eed5da6 |
|
BLAKE2b-256 | 338e7c78d042c05fa252ec7818f643ccffeb766ed41ca6e6be0d971c780e6b0f |