Skip to main content

Flask-Alchemist

Flask extension for working with SQLAlchemy.

It can be used as an alternative for Flask-SQLAlchemy.

Instead of the scoped session tied to a thread you can instantiate database session on demand with a context manager. The advantage is that when the Flask application handles a request you can precisely control when the session starts and ends.

There is also a Model class with modified MetaData configuration which tells SQLAlchemy how to name indexes and constraints in a database. You can subclass it to define your own model classes.

The Model class provides also show_column method to display information about columns defined in a model (it helps when you want to create database objects in the Flask shell).

Installation

$ pip install Flask-Alchemist

Usage

First create the db object:

from flask_alchemist import Alchemist
db = Alchemist()

Then initialize it using init_app method:

db.init_app(app)  # app is your Flask app instance

The Flask app config should have DATABASE_URL key. That is a connection string that tells SQLAlchemy what database to connect to.

Using the db object you can create database session on demand in your views:

with db.Session() as db_session:
    user = db_session.get(User, obj_id)

The session is closed when the context manager block ends.

When you want to modify database you can combine two context managers:

with db.Session() as db_session:
    with db_session.begin():
        db_session.add(obj)

or

with db.Session() as db_session, db_session.begin():
    db_session.add(obj)

The session automatically commits (inner context manager) and closes (outer context manager) at the end. If an error occurs inside the inner context manager the session is rolled back.

After defining your User model class you can inspect it in the Flask shell:

>>> User.show_columns()
Column('id', Integer(), table=<users>, primary_key=True, nullable=False)
Column('username', String(length=64), table=<users>, nullable=False)
Column('password_hash', String(length=162), table=<users>)
Column('first_name', String(length=64), table=<users>, nullable=False)
Column('last_name', String(length=64), table=<users>, nullable=False)
Column('email', String(length=64), table=<users>, nullable=False, default=ScalarElementColumnDefault(''))
Column('active', Boolean(), table=<users>, nullable=False, default=ScalarElementColumnDefault(True))

Other features

Alchemist object can be used as a proxy for any attribute of the SQLAlchemy Core and the SQLAlchemy ORM. For example when making SQL queries you can use db.select instead of importing this from SQLAlchemy.

There is also a Pagination class for paging query results (similar to Flask-SQLAlchemy).

License

Flask-Alchemist was created by Rafal Padkowski. It is licensed under the terms of the MIT license.

Metadata

Release files for Flask-Alchemist 1.0.2

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

Source distribution (sdist)

Source distribution for Flask-Alchemist 1.0.2
File Size Uploaded
flask_alchemist-1.0.2.tar.gz 3.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for Flask-Alchemist 1.0.2
File Interpreter ABI Platform
flask_alchemist-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 8.1 kB

Release files / flask_alchemist-1.0.2.tar.gz

Download URL flask_alchemist-1.0.2.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
170973b4ce494a3ff1ffc22605397b297bb370e68362d99fd3ae68209a281fa3
BLAKE2b-256 checksum
How to use checksums
ee36134a912d5052f49d54faa206389896b777828140ac30fb0555ce689c11ec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.12 Linux/5.15.0-79-generic

Release files / flask_alchemist-1.0.2-py3-none-any.whl

Download URL flask_alchemist-1.0.2-py3-none-any.whl
Size 4.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
91d55b7d39878d2cc8dfb7c482ab6e722b17466a80850db526cce8cda226e452
BLAKE2b-256 checksum
How to use checksums
8656c4d0f7a19badb4471987ebaee0da34e94b5490d12e97c7d0cb60ef1e57aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.7.1 CPython/3.10.12 Linux/5.15.0-79-generic

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.2.0

2 release files

0.1.1

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