sqlacodegen

Automatic model code generator for SQLAlchemy

https://github.com/agronholm/sqlacodegen

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (13.9%) to scientific vocabulary

Keywords from Contributors

alignment flexible profiles energy-system-model
Last synced: 10 months ago · JSON representation

Repository

Automatic model code generator for SQLAlchemy

Basic Info
  • Host: GitHub
  • Owner: agronholm
  • License: other
  • Language: Python
  • Default Branch: master
  • Homepage:
  • Size: 491 KB
Statistics
  • Stars: 2,192
  • Watchers: 28
  • Forks: 278
  • Open Issues: 42
  • Releases: 5
Created over 9 years ago · Last pushed 10 months ago
Metadata Files
Readme Changelog Contributing License

README.rst

.. 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 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.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/


Installation
============

To install, do::

    pip install sqlacodegen

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[citext]


To include support for the PostgreSQL ``GEOMETRY``, ``GEOGRAPHY``, and ``RASTER`` types
(which should be considered as tested only under a few environments) specify the
``geoalchemy2`` extra:

To include support for the PostgreSQL ``PGVECTOR`` extension type, specify the
``pgvector`` extra::

    pip install sqlacodegen[pgvector]

.. code-block:: bash

    pip install sqlacodegen[geoalchemy2]


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 postgresql:///some_local_db
    sqlacodegen --generator tables mysql+pymysql://user:password@localhost/dbname
    sqlacodegen --generator dataclasses sqlite:///database.db
    # --engine-arg values are parsed with ast.literal_eval
    sqlacodegen oracle+oracledb://user:pass@127.0.0.1:1521/XE --engine-arg thick_mode=True
    sqlacodegen oracle+oracledb://user:pass@127.0.0.1:1521/XE --engine-arg thick_mode=True --engine-arg connect_args='{"user": "user", "dsn": "..."}'

To see the list of generic 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

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()``
* ``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 ``--options`` (multiple
values must be delimited by commas, e.g. ``--options noconstraints,nobidi``):

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

* ``sqlmodels``

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

Getting help
============

If you have problems or other questions, you should start a discussion on the
`sqlacodegen discussion forum`_. As an alternative, you could also try your luck on the
sqlalchemy_ room on Gitter.

.. _sqlacodegen discussion forum: https://github.com/agronholm/sqlacodegen/discussions/categories/q-a
.. _sqlalchemy: https://app.gitter.im/#/room/#sqlalchemy_community:gitter.im

Owner

  • Name: Alex Grönholm
  • Login: agronholm
  • Kind: user
  • Location: Nurmijärvi, Finland
  • Company: NextDay Solutions Oy

GitHub Events

Total
  • Create event: 19
  • Release event: 1
  • Issues event: 59
  • Watch event: 271
  • Delete event: 22
  • Member event: 1
  • Issue comment event: 315
  • Push event: 63
  • Pull request review comment event: 153
  • Pull request review event: 163
  • Pull request event: 71
  • Fork event: 31
Last Year
  • Create event: 19
  • Release event: 1
  • Issues event: 59
  • Watch event: 271
  • Delete event: 22
  • Member event: 1
  • Issue comment event: 315
  • Push event: 63
  • Pull request review comment event: 153
  • Pull request review event: 163
  • Pull request event: 71
  • Fork event: 31

Committers

Last synced: about 1 year ago

All Time
  • Total Commits: 354
  • Total Committers: 23
  • Avg Commits per committer: 15.391
  • Development Distribution Score (DDS): 0.209
Past Year
  • Commits: 29
  • Committers: 6
  • Avg Commits per committer: 4.833
  • Development Distribution Score (DDS): 0.552
Top Committers
Name Email Commits
Alex Grönholm a****m@n****i 280
pre-commit-ci[bot] 6****] 49
Idan Sheinberg i****0@g****m 2
Laurent Savaete l****t@w****f 2
Leonardus Chen l****n@g****m 2
d10n d****d@b****m 2
AG g****b@m****m 1
Andrew Carretta a****w@a****m 1
Andrii Khirilov 5****A 1
Dan O'Huiginn d****l@o****t 1
Dave Hirschfeld d****d@g****m 1
JoaquimEsteves j****e@h****m 1
Jonathan Poulter p****7@g****m 1
Lajos Cseppentő 1****o 1
Markus Hauru m****s@m****g 1
Neil Halelamien n****a@g****m 1
Nicholas Martin n****2@g****m 1
Plazmer p****3@g****m 1
Tom White t****9@p****m 1
Wayne Nilsen w****n@g****m 1
amacfie-tc 9****c 1
softwarepk s****k 1
stavvy-rotte 1****e 1
Committer Domains (Top 20 + Academic)

Issues and Pull Requests

Last synced: 10 months ago

All Time
  • Total issues: 120
  • Total pull requests: 156
  • Average time to close issues: 9 months
  • Average time to close pull requests: about 2 months
  • Total issue authors: 95
  • Total pull request authors: 45
  • Average comments per issue: 4.13
  • Average comments per pull request: 2.6
  • Merged pull requests: 99
  • Bot issues: 1
  • Bot pull requests: 74
Past Year
  • Issues: 25
  • Pull requests: 66
  • Average time to close issues: 16 days
  • Average time to close pull requests: 5 days
  • Issue authors: 20
  • Pull request authors: 15
  • Average comments per issue: 3.16
  • Average comments per pull request: 2.52
  • Merged pull requests: 43
  • Bot issues: 1
  • Bot pull requests: 26
Top Authors
Issue Authors
  • amacfie (6)
  • laurentS (4)
  • henrikmathisen (3)
  • sheinbergon (3)
  • Distortedlogic (3)
  • leonarduschen (3)
  • agronholm (3)
  • PaleNeutron (3)
  • krr0land (2)
  • devspacenine (2)
  • Haladesta (2)
  • simkessy (2)
  • jensenbox (2)
  • ZeeD (1)
  • Vrushank-21 (1)
Pull Request Authors
  • pre-commit-ci[bot] (72)
  • agronholm (9)
  • sheinbergon (9)
  • MRigal (4)
  • stavvy-rotte (3)
  • amacfie (3)
  • leonarduschen (3)
  • poulter7 (2)
  • dependabot[bot] (2)
  • laurentS (2)
  • Henkhogan (2)
  • LajosCseppento (2)
  • THUzxj (2)
  • np-kyokyo (2)
  • multimeric (2)
Top Labels
Issue Labels
bug (53) enhancement (24) duplicate (2) help wanted (2) triage-required (2) invalid (1) wontfix (1) not-a-bug (1) major (1) critical (1)
Pull Request Labels
dependencies (2) enhancement (2)

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 230,431 last-month
  • Total docker downloads: 618
  • Total dependent packages: 10
  • Total dependent repositories: 407
  • Total versions: 25
  • Total maintainers: 1
pypi.org: sqlacodegen

Automatic model code generator for SQLAlchemy

  • Versions: 25
  • Dependent Packages: 10
  • Dependent Repositories: 407
  • Downloads: 230,431 Last month
  • Docker Downloads: 618
Rankings
Dependent repos count: 0.7%
Downloads: 1.0%
Dependent packages count: 1.6%
Stargazers count: 1.7%
Average: 1.9%
Docker downloads count: 2.6%
Forks count: 3.6%
Maintainers (1)
Last synced: 10 months ago

Dependencies

.github/workflows/publish.yml actions
  • actions/checkout v2 composite
  • actions/setup-python v2 composite
  • pypa/gh-action-pypi-publish master composite
.github/workflows/test.yml actions
  • actions/cache v2 composite
  • actions/checkout v2 composite
  • actions/setup-python v2 composite
pyproject.toml pypi
  • SQLAlchemy >= 1.4.36
  • greenlet >= 3.0.0a1; python_version >= '3.12'
  • importlib_metadata python_version < '3.10'
  • inflect >= 4.0.0