Support the compatibility between Flask-SQLAlchemy and Flask-SQLAlchemy-Lite.
Project description
Flask SQLAlchemy Compat
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.0.0only. If you are using an older version. You cannot use this package.
[!WARNING] To make this package work with
python=3.7, users should install an unofficialflask-sqlalchemyversion.
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 unofficialflask-sqlalchemyversion. Seehttps://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]
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"))
app.config.update({"SQLALCHEMY_ENGINES": {"default": "sqlite:///main.db"}})
db.init_app(app)
with app.app_context():
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"))
app.config.update({"SQLALCHEMY_DATABASE_URI": "sqlite:///main.db"})
db.init_app(app)
with app.app_context():
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.
Compared to the above minimal examples, we also provided a usage.py file and example applications in the examples/ folder. Check them to view more details.
[!TIP] To run the demos in
examples, you need to install the optional dependencies bypython -m pip install flask-sqlalchemy-compat[example,backends]
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
5. Changelog
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flask_sqlalchemy_compat-0.2.0.tar.gz.
File metadata
- Download URL: flask_sqlalchemy_compat-0.2.0.tar.gz
- Upload date:
- Size: 148.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
007a66dedc463c6f76644e3876e96980b8d90219dddae4defe8a4571c8211245
|
|
| MD5 |
4f7631eb51d975c861ed9d198bf6a2f9
|
|
| BLAKE2b-256 |
ea23143f697187414fb08e5ff538b9540ca1bc7c52e0050fdf266bf6e9cfd066
|
Provenance
The following attestation bundles were made for flask_sqlalchemy_compat-0.2.0.tar.gz:
Publisher:
python-publish.yml on cainmagi/flask-sqlalchemy-compat
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flask_sqlalchemy_compat-0.2.0.tar.gz -
Subject digest:
007a66dedc463c6f76644e3876e96980b8d90219dddae4defe8a4571c8211245 - Sigstore transparency entry: 155250890
- Sigstore integration time:
-
Permalink:
cainmagi/flask-sqlalchemy-compat@0b89349b0917f6611b4cbdcb910186a4ec6e5cf9 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/cainmagi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@0b89349b0917f6611b4cbdcb910186a4ec6e5cf9 -
Trigger Event:
release
-
Statement type:
File details
Details for the file Flask_SQLAlchemy_compat-0.2.0-py3-none-any.whl.
File metadata
- Download URL: Flask_SQLAlchemy_compat-0.2.0-py3-none-any.whl
- Upload date:
- Size: 29.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.0.1 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
748b75cd240a0c6420f41ed9341987c826ef8a7d3c880ba715c46a19bfa0e6ad
|
|
| MD5 |
00b012f643f6a0ab4d868e2533de1f94
|
|
| BLAKE2b-256 |
600b98216ac3ea6201184b8722761876458c5b0c38fb2abd82f4246a2086ce36
|
Provenance
The following attestation bundles were made for Flask_SQLAlchemy_compat-0.2.0-py3-none-any.whl:
Publisher:
python-publish.yml on cainmagi/flask-sqlalchemy-compat
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
flask_sqlalchemy_compat-0.2.0-py3-none-any.whl -
Subject digest:
748b75cd240a0c6420f41ed9341987c826ef8a7d3c880ba715c46a19bfa0e6ad - Sigstore transparency entry: 155250891
- Sigstore integration time:
-
Permalink:
cainmagi/flask-sqlalchemy-compat@0b89349b0917f6611b4cbdcb910186a4ec6e5cf9 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/cainmagi
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@0b89349b0917f6611b4cbdcb910186a4ec6e5cf9 -
Trigger Event:
release
-
Statement type: