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

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.0
File Size Uploaded
sa2django-0.1.0.tar.gz 7.9 kB Details

Built distribution (wheel)

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

Total release size: 15.5 kB

Release files / sa2django-0.1.0.tar.gz

Download URL sa2django-0.1.0.tar.gz
Size 7.9 kB
Tags Source
SHA-256 checksum
How to use checksums
076ab9910b3c5614805c8bb9da162e76fe5cc77723a239897d332d4b980e5ee7
BLAKE2b-256 checksum
How to use checksums
fe48aa5ce2d83291d17fb0d5eeeb8a8e27d4fdc6ab82d542fbb5a35f3d7b0f4c
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.52.0 CPython/3.8.0

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

Download URL sa2django-0.1.0-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2c329cc6b337e6ff536591828321e8ba1ef94e283768d9d0c4c956b87dc19d78
BLAKE2b-256 checksum
How to use checksums
117dc151e86c4a5370d3467a8fdc5df2999fd31f02b101e3bf7b0a694bcb4385
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.52.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

0.1.1

2 release files

This release

0.1.0 This release

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