Skip to main content

Support the compatibility between `flask_sqlalchemy` and `flask_sqlalchemy_lite`.

Project description

Flask SQLAlchemy Compat

Banner

GitHub release (latest SemVer) GitHub all releases GitHub PyPI - Downloads

GitHub Actions (Build) GitHub Actions (Release)

Support the compatibility between flask_sqlalchemy and flask_sqlalchemy_lite. It allows users to make minimal changes when they need to migrate from either one of these two packages to each other.

The main motivation of this package is because flask_sqlalchemy_lite does not support python<=3.8. This package is designed for providing the similar usages when users have to make the flask_sqlalchemy_lite working with python<=3.8 by using flask_sqlalchemy. In this case, users can get rid of the difficulty of maintaining two sets of codes.

[!WARNING] This package is designed for sqlalchemy>=2. If you are using an older version.

[!WARNING] To make this package work with python=3.7, users should install an unofficial flask-sqlalchemy version.

1. Install

Intall the latest released version of this package by using the PyPI source:

python -m pip install flask-sqlalchemy-compat

Or use the following commands to install the developing version from the GitHub Source when you have already installed Git :hammer::

python -m pip install "flask-sqlalchemy-compat[dev] @ git+https://github.com/cainmagi/flask-sqlalchemy-compat.git"

[!WARNING] To make this package work with python=3.7, users should install an unofficial flask-sqlalchemy version. See

https://github.com/pallets-eco/flask-sqlalchemy/issues/1140#issuecomment-1577921154

This unofficial version can be installed by:

python -m pip install flask-sqlalchemy-compat[backends]

Note that specifying the option backends require git when you are using python=3.7. Certainly, you do not need git if you are using python>=3.8.

2. Usage

When you are using flask-sqlalchemy-lite, using the following codes will let your codes fall back to the compatible mode if flask-sqlalchemy-lite is not installed but flask-sqlalchemy is installed.

import sqlalchemy as sa
import sqlalchemy.orm as sa_orm
import flask_sqlalchemy_compat as fsc


class _Base(sa_orm.DeclarativeBase): ...


db, Base = fsc.get_flask_sqlalchemy_lite(_Base)


class NewModel(Base):
    __tablename__ = "new_model"

    id: sa_orm.Mapped[int] = sa_orm.mapped_column(primary_key=True)
    name: sa_orm.Mapped[str] = sa_orm.mapped_column()


if __name__ == "__main__":
    import os
    import flask

    app = flask.Flask(__name__, instance_path=os.path.abspath("./instance"))

    with app.app_context():
        app.config.update({"SQLALCHEMY_ENGINES": {"default": "sqlite:///main.db"}})
        db.init_app(app)

        Base.metadata.create_all(db.engine)

        db.session.add(NewModel(name="new"))
        db.session.commit()

        model = db.session.scalar(sa.select(NewModel))
        print(model.id, model.name) if model is not None else print("NOT FOUND.")

The above codes will works no matter flask_sqlalchemy_lite or flask_sqlalchemy is installed.

Similarly, the following example is designed for the codes written with flask_sqlalchemy. The codes fall back to the compatible mode if flask-sqlalchemy is not installed but flask-sqlalchemy-lite is installed.

import sqlalchemy as sa
import sqlalchemy.orm as sa_orm
import flask_sqlalchemy_compat as fsc


class _Base(sa_orm.DeclarativeBase): ...


db = fsc.get_flask_sqlalchemy(_Base)


class NewModel(db.Model):
    id: sa_orm.Mapped[int] = sa_orm.mapped_column(primary_key=True)
    name: sa_orm.Mapped[str] = sa_orm.mapped_column()


if __name__ == "__main__":
    import os
    import flask

    app = flask.Flask(__name__, instance_path=os.path.abspath("./instance"))

    with app.app_context():
        app.config.update({"SQLALCHEMY_DATABASE_URI": "sqlite:///main.db"})
        db.init_app(app)
        db.create_all()

        # Indeed, flask_sqlalchemy has a type hint issue until `3.1.x`.
        # But it does not cause issues in run time. See
        # https://github.com/pallets-eco/flask-sqlalchemy/issues/1312
        db.session.add(NewModel(name="new"))
        db.session.commit()

        model = db.session.scalar(sa.select(NewModel))
        print(model.id, model.name) if model is not None else print("NOT FOUND.")

The magic happens if you run the first example with only flask-sqlalchemy installed and the second example with only flask-sqlalchemy-lite installed.

3. Documentation

[!CAUTION] The documentation is not accessible now because it is still to be done.

Check the documentation to find more details about the examples and APIs.

https://cainmagi.github.io/flask-sqlalchemy-compat/

4. Contributing

See CONTRIBUTING.md :book:

5. Changelog

See Changelog.md :book:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flask_sqlalchemy_compat-0.1.0.tar.gz (92.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Flask_SQLAlchemy_compat-0.1.0-py3-none-any.whl (26.8 kB view details)

Uploaded Python 3

File details

Details for the file flask_sqlalchemy_compat-0.1.0.tar.gz.

File metadata

  • Download URL: flask_sqlalchemy_compat-0.1.0.tar.gz
  • Upload date:
  • Size: 92.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for flask_sqlalchemy_compat-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e71f8af42e4e5d788411cc922e97487b9b971508982af0a4c52bbcc0cbf18e77
MD5 2806e4035d30f4410d54ca7008682a40
BLAKE2b-256 3eec724d068d06e96ab985e51df3c32d31ba50c461cf1225435fb9a5744130ef

See more details on using hashes here.

Provenance

The following attestation bundles were made for flask_sqlalchemy_compat-0.1.0.tar.gz:

Publisher: python-publish.yml on cainmagi/flask-sqlalchemy-compat

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file Flask_SQLAlchemy_compat-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for Flask_SQLAlchemy_compat-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 538b981907f633e4b65efcb236d92ff945ed71750b5981c9c5d17508d2d3e749
MD5 3b75cdaf536a3cc2e02b6eb9fa4d8e87
BLAKE2b-256 ba9c8448feafb3102d9146e23bce87c96c729cb92014bf07242692f7daf352ab

See more details on using hashes here.

Provenance

The following attestation bundles were made for Flask_SQLAlchemy_compat-0.1.0-py3-none-any.whl:

Publisher: python-publish.yml on cainmagi/flask-sqlalchemy-compat

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page