Automatic model code generator for SQLAlchemy
The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:
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).
.. _sqlautocode: http://code.google.com/p/sqlautocode/
PEP 8_ compliant code
.. _PEP 8: http://www.python.org/dev/peps/pep-0008/
To install, do::
pip install sqlacodegen
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.
sqlacodegen postgresql:///some_local_db sqlacodegen mysql+oursql://user:[email protected]/dbname sqlacodegen sqlite:///database.db
To see the full list of options::
.. createengine(): http://docs.sqlalchemy.org/en/latest/core/engines.html#sqlalchemy.create_engine .. _SQLAlchemy's documentation: http://docs.sqlalchemy.org/en/latest/core/engines.html
--noclassesoption is used, sqlacodegen tries to generate declarative model classes from each table. There are two circumstances in which a
Tableis generated instead:
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,
Relationships are detected based on existing foreign key constraints as follows:
A table is considered an association table if it satisfies all of the following conditions:
Relationships are typically named based on the opposite class name. For example, if an
Employeeclass has a column named
employerwhich has a foreign key to
Company.id, the relationship is named
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
employerdue to that
If more than one relationship would be created with the same name, the latter ones are appended numeric suffixes, starting from 1.
If you have problems or other questions, you can either:
SQLAlchemy Google group_, or
.. SQLAlchemy Google group: http://groups.google.com/group/sqlalchemy .. _Freenode IRC: http://freenode.net/ircservers.shtml