Skip to main content

SqlAlchemy to Django Bridge

Convert sqlalchemy database schemas to django database models at runtime.

Original url: https://github.com/mariushelf/sa2django

The SQLAlchemy to Django Bridge allows to specify your data models in SQLAlchemy and use them in Django, without manually re-specifying all your models and fields for the Django ORM.

Why did you create this package?

Specifying a schema in SQLAlchemy and then using it in Django sounds like... counter- intuitive. There are a lot of Why nots to answer...

Why not specify your models in Django straight away?

We use Django to serve data that is maintained from sources outside of the Django application. Those sources already specify a complete SQLAlchemy model.

Hence we already have a full specification of the data model in SQLAlchemy.

Why not simply use inspectdb to dynamically generate the Django model specifications?

inspectdb is not that dynamic after all -- it generates a Python file once, which needs to be manually tweaked. And each time the data model changes, you need to adjust that Python file.

Also it is often not possible to automatically derive all relations between models from the database. With third-party datasources, often relations are not manifested as foreign key constraints in the database schema, but just in some documentation that explains the relations in human-, but not machine-readable form.

Our SQLAlchemy models already contain all those relations, and it makes sense to convert the SQLAlchemy models to Django ORM at runtime.

Status

The SQLAlchemy to Django Bridge works well for our use case.

There are probably a lot of corner cases and advanced (or not so advanced) features of SQLAlchemy that are not (yet) supported.

Installation

pip install sa2django

Features

  • basic data types (int, float, string, varchar, char, date, datetime etc, bytea)
  • foreign keys and many-to-one relationships
  • many-to-many relationships including through tables
  • automatic inference of all declared models in a sqlalchemy Base
  • alternatively define your Django models as usual, but use the SA2DModel as base class. Then all database fields are added from the corresponding sqlalchemy model, but you can still add properties and functions to the Django model

Limitations

SQLAlchemy provides a superset of Django's functionality. For this reason, there's a long list of limitations.

The list is even longer and probably not exhaustive because sa2django is a young project tailored to its author's current needs.

  • at the moment only declarative base definitions are supported, no pure Mapper objects
  • composite foreign keys and primary keys are not supported. Primary keys and foreign keys must contain exactly one column

Many to many relationships

  • In sqlalchemy, in the mapper of the intermediate table, both foreign keys and relationships linking to both tables must be specified.

    Example:

    class CarParentAssoc(Base):
        __tablename__ = "cartoparent"
        id = Column(Integer, primary_key=True)
        car = relationship("Car")
        parent = relationship("Parent")
        car_id = Column(Integer, ForeignKey("car.car_id"))
        parent_id = Column(Integer, ForeignKey("parent.id"))
    

    Note that for both links to the car and parent table, both foreign keys and relationship attributes are specified.

Contributing

Pull requests are more than welcome! Ideally reach out to us by creating or replying to a Github ticket such that we can align our work and ideas.

License

MIT

Author: Marius Helf (helfsmarius@gmail.com)

Metadata

Release files for sa2django 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sa2django 0.1.1
File Size Uploaded
sa2django-0.1.1.tar.gz 8.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for sa2django 0.1.1
File Interpreter ABI Platform
sa2django-0.1.1-py3-none-any.whl Python 3 none any Details

Total release size: 15.7 kB

Release files / sa2django-0.1.1.tar.gz

Download URL sa2django-0.1.1.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
3075869ed91367fc9fc4258321248ab9e75cc5058befc98c89b91ce467c8559b
BLAKE2b-256 checksum
How to use checksums
e7550d291e3ed593be9726cc25e94635e1f7db89ed3fc1848edbcbb405a8ae51
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.2.0 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.0

Release files / sa2django-0.1.1-py3-none-any.whl

Download URL sa2django-0.1.1-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a081fe5123fb5b55df58403f5a8c38b0cec84b62c1dce197048ff8d85c3b440c
BLAKE2b-256 checksum
How to use checksums
f1ea39055de3881594e22b411fa7225c341d963297f688426a413e9d226ac65a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.2.0 requests-toolbelt/0.9.1 tqdm/4.53.0 CPython/3.8.0

Release history Release notifications | RSS feed

0.2.1

2 release files

0.2.0

2 release files

0.1.3

2 release files

0.1.2

2 release files

This release

0.1.1 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page