Name | sqlacodegen-v2 JSON |
Version |
0.1.4
JSON |
| download |
home_page | |
Summary | Automatic model code generator for SQLAlchemy 2.0 |
upload_time | 2023-06-13 19:32:30 |
maintainer | |
docs_url | None |
author | |
requires_python | >=3.7 |
license | MIT |
keywords |
sqlalchemy
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
.. image:: https://github.com/agronholm/sqlacodegen/actions/workflows/test.yml/badge.svg
:target: https://github.com/agronholm/sqlacodegen/actions/workflows/test.yml
:alt: Build Status
.. image:: https://coveralls.io/repos/github/agronholm/sqlacodegen/badge.svg?branch=master
:target: https://coveralls.io/github/agronholm/sqlacodegen?branch=master
:alt: Code Coverage
This is an fork project from https://github.com/agronholm/sqlacodegen
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 2.0
* 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/
Installation
============
To install, do::
pip install sqlacodegen_v2
To include support for the PostgreSQL ``CITEXT`` extension type (which should be
considered as tested only under a few environments) specify the ``citext`` extra::
pip install sqlacodegen_v2[citext]
Quickstart
==========
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_v2 postgresql:///some_local_db
sqlacodegen_v2 --generator tables mysql+pymysql://user:password@localhost/dbname
sqlacodegen_v2 --generator dataclasses sqlite:///database.db
To see the list of generic options::
sqlacodegen_v2 --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
Available generators
====================
The selection of a generator determines the
The following built-in generators are available:
* ``tables`` (only generates ``Table`` objects, for those who don't want to use the ORM)
* ``declarative`` (the default; generates classes inheriting from ``declarative_base()``
* ``declarative-dataclasses`` (generate declarative model with Superclass ``MappedAsDataclass``)
* ``dataclasses`` (generates dataclass-based models; v1.4+ only)
* ``sqlmodels`` (generates model classes for SQLModel_)
.. _SQLModel: https://sqlmodel.tiangolo.com/
Generator-specific options
==========================
The following options can be turned on by passing them using ``--option`` (can be used
multiple times):
* ``tables``
* ``noconstraints``: ignore constraints (foreign key, unique etc.)
* ``nocomments``: ignore table/column comments
* ``noindexes``: ignore indexes
* ``declarative``
* all the options from ``tables``
* ``use_inflect``: use the ``inflect`` library when naming classes and relationships
(turning plural names into singular; see below for details)
* ``nojoined``: don't try to detect joined-class inheritance (see below for details)
* ``nobidi``: generate relationships in a unidirectional fashion, so only the
many-to-one or first side of many-to-many relationships gets a relationship
attribute, as on v2.X
* ``dataclasses``
* all the options from ``declarative``
* ``sqlmodel``
* all the options from ``declarative``
Model class generators
----------------------
The code generators that generate classes try to generate model classes whenever
possible. 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
++++++++++++++++++++++++
By default, table names are converted to valid PEP 8 compliant class names by replacing
all characters unsuitable for Python identifiers with ``_``. Then, each valid parts
(separated by underscores) are title cased and then joined together, eliminating the
underscores. So, ``example_name`` becomes ``ExampleName``.
If the ``use_inflect`` option is used, the table name (which is assumed to be in
English) is converted to singular form using the "inflect" library. For example,
``sales_invoices`` becomes ``SalesInvoice``. Since table names are not always in
English, and the inflection process is far from perfect, inflection is disabled by
default.
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**: (not implemented on the ``sqlmodel`` generator) 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 table name of the opposite class.
For example, if a class has a relationship to another class with the table named
``companies``, the relationship would be named ``companies`` (unless the ``use_inflect``
option was enabled, in which case it would be named ``company`` in the case of a
many-to-one or one-to-one relationship).
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.
For self referential relationships, the reverse side of the relationship will be named
with the ``_reverse`` suffix appended to it.
Customizing code generation logic
=================================
If the built-in generators with all their options don't quite do what you want, you can
customize the logic by subclassing one of the existing code generator classes. Override
whichever methods you need, and then add an `entry point`_ in the
``sqlacodegen.generators`` namespace that points to your new class. Once the entry point
is in place (you typically have to install the project with ``pip install``), you can
use ``--generator <yourentrypoint>`` to invoke your custom code generator.
For examples, you can look at sqlacodegen's own entry points in its `pyproject.toml`_.
.. _entry point: https://setuptools.readthedocs.io/en/latest/userguide/entry_point.html
.. _pyproject.toml: https://github.com/agronholm/sqlacodegen/blob/master/pyproject.toml
Customizing Workflow
=================================
You may import ``generate_models`` directly from ``sqlacodegen_v2``
Raw data
{
"_id": null,
"home_page": "",
"name": "sqlacodegen-v2",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.7",
"maintainer_email": "",
"keywords": "sqlalchemy",
"author": "",
"author_email": "Chengkai Mai <c.mai@madainchina.com>",
"download_url": "https://files.pythonhosted.org/packages/04/f1/1317ac2aa548e1d920526c6fb6d690650faa5ccf034b1d550e759c1808f1/sqlacodegen_v2-0.1.4.tar.gz",
"platform": null,
"description": ".. image:: https://github.com/agronholm/sqlacodegen/actions/workflows/test.yml/badge.svg\n :target: https://github.com/agronholm/sqlacodegen/actions/workflows/test.yml\n :alt: Build Status\n.. image:: https://coveralls.io/repos/github/agronholm/sqlacodegen/badge.svg?branch=master\n :target: https://coveralls.io/github/agronholm/sqlacodegen?branch=master\n :alt: Code Coverage\n\nThis is an fork project from https://github.com/agronholm/sqlacodegen\n\nThis is a tool that reads the structure of an existing database and generates the\nappropriate SQLAlchemy model code, using the declarative style if possible.\n\nThis tool was written as a replacement for `sqlautocode`_, which was suffering from\nseveral issues (including, but not limited to, incompatibility with Python 3 and the\nlatest SQLAlchemy version).\n\n.. _sqlautocode: http://code.google.com/p/sqlautocode/\n\n\nFeatures\n========\n\n* Supports SQLAlchemy 2.0\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\nInstallation\n============\n\nTo install, do::\n\n pip install sqlacodegen_v2\n\nTo include support for the PostgreSQL ``CITEXT`` extension type (which should be\nconsidered as tested only under a few environments) specify the ``citext`` extra::\n\n pip install sqlacodegen_v2[citext]\n\n\nQuickstart\n==========\n\nAt the minimum, you have to give sqlacodegen a database URL. The URL is passed directly\nto SQLAlchemy's `create_engine()`_ method so please refer to\n`SQLAlchemy's documentation`_ for instructions on how to construct a proper URL.\n\nExamples::\n\n sqlacodegen_v2 postgresql:///some_local_db\n sqlacodegen_v2 --generator tables mysql+pymysql://user:password@localhost/dbname\n sqlacodegen_v2 --generator dataclasses sqlite:///database.db\n\nTo see the list of generic options::\n\n sqlacodegen_v2 --help\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\nAvailable generators\n====================\n\nThe selection of a generator determines the\n\nThe following built-in generators are available:\n\n* ``tables`` (only generates ``Table`` objects, for those who don't want to use the ORM)\n* ``declarative`` (the default; generates classes inheriting from ``declarative_base()``\n* ``declarative-dataclasses`` (generate declarative model with Superclass ``MappedAsDataclass``)\n* ``dataclasses`` (generates dataclass-based models; v1.4+ only)\n* ``sqlmodels`` (generates model classes for SQLModel_)\n\n.. _SQLModel: https://sqlmodel.tiangolo.com/\n\nGenerator-specific options\n==========================\n\nThe following options can be turned on by passing them using ``--option`` (can be used\nmultiple times):\n\n* ``tables``\n\n * ``noconstraints``: ignore constraints (foreign key, unique etc.)\n * ``nocomments``: ignore table/column comments\n * ``noindexes``: ignore indexes\n\n* ``declarative``\n\n * all the options from ``tables``\n * ``use_inflect``: use the ``inflect`` library when naming classes and relationships\n (turning plural names into singular; see below for details)\n * ``nojoined``: don't try to detect joined-class inheritance (see below for details)\n * ``nobidi``: generate relationships in a unidirectional fashion, so only the\n many-to-one or first side of many-to-many relationships gets a relationship\n attribute, as on v2.X\n\n* ``dataclasses``\n\n * all the options from ``declarative``\n\n* ``sqlmodel``\n\n * all the options from ``declarative``\n\nModel class generators\n----------------------\n\nThe code generators that generate classes try to generate model classes whenever\npossible. 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\n model class)\n* the table is an association table between two other tables (see below for the\n specifics)\n\nModel class naming logic\n++++++++++++++++++++++++\n\nBy default, table names are converted to valid PEP 8 compliant class names by replacing\nall characters unsuitable for Python identifiers with ``_``. Then, each valid parts\n(separated by underscores) are title cased and then joined together, eliminating the\nunderscores. So, ``example_name`` becomes ``ExampleName``.\n\nIf the ``use_inflect`` option is used, the table name (which is assumed to be in\nEnglish) is converted to singular form using the \"inflect\" library. For example,\n``sales_invoices`` becomes ``SalesInvoice``. Since table names are not always in\nEnglish, and the inflection process is far from perfect, inflection is disabled by\ndefault.\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\n column(s) involved\n* **many-to-many**: (not implemented on the ``sqlmodel`` generator) an association table\n is found to exist between two tables\n\nA table is considered an association table if it satisfies all of the following\nconditions:\n\n#. has exactly two foreign key constraints\n#. all its columns are involved in said constraints\n\nRelationship naming logic\n+++++++++++++++++++++++++\n\nRelationships are typically named based on the table name of the opposite class.\nFor example, if a class has a relationship to another class with the table named\n``companies``, the relationship would be named ``companies`` (unless the ``use_inflect``\noption was enabled, in which case it would be named ``company`` in the case of a\nmany-to-one or one-to-one relationship).\n\nA special case for single column many-to-one and one-to-one relationships, however, is\nif the column is named like ``employer_id``. Then the relationship is named ``employer``\ndue to that ``_id`` suffix.\n\nFor self referential relationships, the reverse side of the relationship will be named\nwith the ``_reverse`` suffix appended to it.\n\nCustomizing code generation logic\n=================================\n\nIf the built-in generators with all their options don't quite do what you want, you can\ncustomize the logic by subclassing one of the existing code generator classes. Override\nwhichever methods you need, and then add an `entry point`_ in the\n``sqlacodegen.generators`` namespace that points to your new class. Once the entry point\nis in place (you typically have to install the project with ``pip install``), you can\nuse ``--generator <yourentrypoint>`` to invoke your custom code generator.\n\nFor examples, you can look at sqlacodegen's own entry points in its `pyproject.toml`_.\n\n.. _entry point: https://setuptools.readthedocs.io/en/latest/userguide/entry_point.html\n.. _pyproject.toml: https://github.com/agronholm/sqlacodegen/blob/master/pyproject.toml\n\nCustomizing Workflow\n=================================\nYou may import ``generate_models`` directly from ``sqlacodegen_v2``\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Automatic model code generator for SQLAlchemy 2.0",
"version": "0.1.4",
"project_urls": {
"Bug Tracker": "https://github.com/maacck/sqlacodegen_v2/issues",
"Source Code": "https://github.com/maacck/sqlacodegen_v2"
},
"split_keywords": [
"sqlalchemy"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "7c823cd9aaa7cd65ce3ac4536de94cb786c045420234a6c0c4d47a633e85edcc",
"md5": "52745491a411408327c27bc46fed0646",
"sha256": "de6bbd548a8cee221fa8870f0710c9154f0d82e388ba4f092d294fd06eed47ed"
},
"downloads": -1,
"filename": "sqlacodegen_v2-0.1.4-py3-none-any.whl",
"has_sig": false,
"md5_digest": "52745491a411408327c27bc46fed0646",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.7",
"size": 22098,
"upload_time": "2023-06-13T19:32:27",
"upload_time_iso_8601": "2023-06-13T19:32:27.803533Z",
"url": "https://files.pythonhosted.org/packages/7c/82/3cd9aaa7cd65ce3ac4536de94cb786c045420234a6c0c4d47a633e85edcc/sqlacodegen_v2-0.1.4-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "04f11317ac2aa548e1d920526c6fb6d690650faa5ccf034b1d550e759c1808f1",
"md5": "feb0f398719bf50ebf1e1bf5d9db527e",
"sha256": "11dca99a23634bc7c3a8b3f8a491203f670810fc8250448ef6ef6a6b5a3bacc1"
},
"downloads": -1,
"filename": "sqlacodegen_v2-0.1.4.tar.gz",
"has_sig": false,
"md5_digest": "feb0f398719bf50ebf1e1bf5d9db527e",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.7",
"size": 35153,
"upload_time": "2023-06-13T19:32:30",
"upload_time_iso_8601": "2023-06-13T19:32:30.069679Z",
"url": "https://files.pythonhosted.org/packages/04/f1/1317ac2aa548e1d920526c6fb6d690650faa5ccf034b1d550e759c1808f1/sqlacodegen_v2-0.1.4.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-06-13 19:32:30",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "maacck",
"github_project": "sqlacodegen_v2",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "sqlacodegen-v2"
}