sqlacodegen


Namesqlacodegen JSON
Version 2.3.0.post1 PyPI version JSON
download
home_page
SummaryAutomatic model code generator for SQLAlchemy
upload_time2023-03-06 07:47:52
maintainer
docs_urlNone
authorAlex Grönholm
requires_python!=3.0,!=3.1,!=3.2,!=3.3,!=3.4,>=2.7
licenseMIT
keywords sqlalchemy
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            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/


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

.. _PEP 8: http://www.python.org/dev/peps/pep-0008/


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


.. _create_engine(): http://docs.sqlalchemy.org/en/latest/core/engines.html#sqlalchemy.create_engine
.. _SQLAlchemy's documentation: http://docs.sqlalchemy.org/en/latest/core/engines.html


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`_

.. _SQLAlchemy Google group: http://groups.google.com/group/sqlalchemy
.. _Freenode IRC: http://freenode.net/irc_servers.shtml

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "sqlacodegen",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "!=3.0,!=3.1,!=3.2,!=3.3,!=3.4,>=2.7",
    "maintainer_email": "",
    "keywords": "sqlalchemy",
    "author": "Alex Gr\u00f6nholm",
    "author_email": "alex.gronholm@nextday.fi",
    "download_url": "https://files.pythonhosted.org/packages/dd/a1/14365d345df9d8abd898f20dc6a84df2db0fb9b03b79576e0dd678716522/sqlacodegen-2.3.0.post1.tar.gz",
    "platform": null,
    "description": "This is a tool that reads the structure of an existing database and generates the appropriate\nSQLAlchemy model code, using the declarative style if possible.\n\nThis tool was written as a replacement for `sqlautocode`_, which was suffering from several issues\n(including, but not limited to, incompatibility with Python 3 and the latest SQLAlchemy version).\n\n.. _sqlautocode: http://code.google.com/p/sqlautocode/\n\n\nFeatures\n========\n\n* Supports SQLAlchemy 0.8.x - 1.3.x\n* Produces declarative code that almost looks like it was hand written\n* Produces `PEP 8`_ compliant code\n* Accurately determines relationships, including many-to-many, one-to-one\n* Automatically detects joined table inheritance\n* Excellent test coverage\n\n.. _PEP 8: http://www.python.org/dev/peps/pep-0008/\n\n\nUsage instructions\n==================\n\nInstallation\n------------\n\nTo install, do::\n\n    pip install sqlacodegen\n\n\nExample usage\n-------------\n\nAt the minimum, you have to give sqlacodegen a database URL. The URL is passed directly to\nSQLAlchemy's `create_engine()`_ method so please refer to `SQLAlchemy's documentation`_ for\ninstructions on how to construct a proper URL.\n\nExamples::\n\n    sqlacodegen postgresql:///some_local_db\n    sqlacodegen mysql+oursql://user:password@localhost/dbname\n    sqlacodegen sqlite:///database.db\n\nTo see the full list of options::\n\n    sqlacodegen --help\n\n\n.. _create_engine(): http://docs.sqlalchemy.org/en/latest/core/engines.html#sqlalchemy.create_engine\n.. _SQLAlchemy's documentation: http://docs.sqlalchemy.org/en/latest/core/engines.html\n\n\nWhy does it sometimes generate classes and sometimes Tables?\n------------------------------------------------------------\n\nUnless the ``--noclasses`` option is used, sqlacodegen tries to generate declarative model classes\nfrom each table. There are two circumstances in which a ``Table`` is generated instead:\n\n* the table has no primary key constraint (which is required by SQLAlchemy for every model class)\n* the table is an association table between two other tables (see below for the specifics)\n\n\nModel class naming logic\n------------------------\n\nThe table name (which is assumed to be in English) is converted to singular form using the\n\"inflect\" library. Then, every underscore is removed while transforming the next letter to upper\ncase. For example, ``sales_invoices`` becomes ``SalesInvoice``.\n\n\nRelationship detection logic\n----------------------------\n\nRelationships are detected based on existing foreign key constraints as follows:\n\n* **many-to-one**: a foreign key constraint exists on the table\n* **one-to-one**: same as **many-to-one**, but a unique constraint exists on the column(s) involved\n* **many-to-many**: an association table is found to exist between two tables\n\nA table is considered an association table if it satisfies all of the following conditions:\n\n#. has exactly two foreign key constraints\n#. all its columns are involved in said constraints\n\n\nRelationship naming logic\n-------------------------\n\nRelationships are typically named based on the opposite class name. For example, if an ``Employee``\nclass has a column named ``employer`` which has a foreign key to ``Company.id``, the relationship\nis named ``company``.\n\nA special case for single column many-to-one and one-to-one relationships, however, is if the\ncolumn is named like ``employer_id``. Then the relationship is named ``employer`` due to that\n``_id`` suffix.\n\nIf more than one relationship would be created with the same name, the latter ones are appended\nnumeric suffixes, starting from 1.\n\n\nGetting help\n============\n\nIf you have problems or other questions, you can either:\n\n* Ask on the `SQLAlchemy Google group`_, or\n* Ask on the ``#sqlalchemy`` channel on `Freenode IRC`_\n\n.. _SQLAlchemy Google group: http://groups.google.com/group/sqlalchemy\n.. _Freenode IRC: http://freenode.net/irc_servers.shtml\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Automatic model code generator for SQLAlchemy",
    "version": "2.3.0.post1",
    "project_urls": {
        "Bug Tracker": "https://github.com/agronholm/sqlacodegen/issues",
        "Source Code": "https://github.com/agronholm/sqlacodegen"
    },
    "split_keywords": [
        "sqlalchemy"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "134389ee0124f76d4e523c22eef66f3c5e6d50f3513c31144f08df7b9237af8b",
                "md5": "913411f30ed3d5f481d2af18c8f14b2e",
                "sha256": "674d58f54559912c2f785276b50a2f9c52ec7ec9d9aea50a118a002392cb17c3"
            },
            "downloads": -1,
            "filename": "sqlacodegen-2.3.0.post1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "913411f30ed3d5f481d2af18c8f14b2e",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": "!=3.0,!=3.1,!=3.2,!=3.3,!=3.4,>=2.7",
            "size": 13216,
            "upload_time": "2023-03-06T07:47:50",
            "upload_time_iso_8601": "2023-03-06T07:47:50.216553Z",
            "url": "https://files.pythonhosted.org/packages/13/43/89ee0124f76d4e523c22eef66f3c5e6d50f3513c31144f08df7b9237af8b/sqlacodegen-2.3.0.post1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "dda114365d345df9d8abd898f20dc6a84df2db0fb9b03b79576e0dd678716522",
                "md5": "c99876884488d360fa0c0bdcf71a1911",
                "sha256": "daf34a2557fad3dad8cc4f6d625b66ac4671cb7e9ec8319eb958b47314872a99"
            },
            "downloads": -1,
            "filename": "sqlacodegen-2.3.0.post1.tar.gz",
            "has_sig": false,
            "md5_digest": "c99876884488d360fa0c0bdcf71a1911",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "!=3.0,!=3.1,!=3.2,!=3.3,!=3.4,>=2.7",
            "size": 21017,
            "upload_time": "2023-03-06T07:47:52",
            "upload_time_iso_8601": "2023-03-06T07:47:52.391242Z",
            "url": "https://files.pythonhosted.org/packages/dd/a1/14365d345df9d8abd898f20dc6a84df2db0fb9b03b79576e0dd678716522/sqlacodegen-2.3.0.post1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-03-06 07:47:52",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "agronholm",
    "github_project": "sqlacodegen",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "sqlacodegen"
}
        
Elapsed time: 0.21219s