Skip to main content

SQLAlchemyProtocol

TypeSource

db: SQLAlchemyProtocol

Protocol compatible with Flask SQLAlchemy.

Flask SQLAlchemy has been maintained for many years. It has a big community and many users, granting many available plugins that have served various applications for long. However, it works better with SQLAlchemy<2. For the new SQLAlchemy package heavily dependent on the modern typing system, this old extension pacakge may have several unsolved compatibility issues.

Note that users still have to use it if they are working with Python<3.9 because Flask SQLAlchemy Lite is not available for the legacy Python versions.

This protocol covers most but does not include all functionalities of flask_sqlalchemy.SQLAlchemy.

Aliases

This type can be acquired by

import flask_sqlalchemy_compat as fsc


fsc.SQLAlchemyProtocol
fsc.protocols.SQLAlchemyProtocol

Protocol methods

init_app

db.init_app(app: Flask)

Initialize a Flask application for use with this extension instance. This must be called before accessing the database engine or session with the app.

Protocol properties

engine

db.engine: sa.engine.Engine

The default engine associated with the current application.


engines

db.engines: Mapping[str | None, sa.engine.Engine]

Map of bind keys to sqlalchemy.engine.Engine instances for current application. The None key refers to the default engine.


session

db.session: sa_orm.scoped_session[Any]

The default session for the current application context. It will be closed when the context ends.


metadatas

db.metadatas: Mapping[str | None, sa.MetaData]

Map of bind keys to sqlalchemy.schema.MetaData instances. The None key refers to the default metadata.


metadata

db.metadata: sa.MetaData

The default metadata used by Model and Table if no bind key is set.


Query

db.Query: Callable[..., sa.orm.Query]

The default query class used by Model.query and lazy="dynamic" relationships.


Table

db.Table: Callable[..., sa.Table]

The default data table class that does not require user-specified metadata.


Model

db.Model: Callable[..., ModelProtocol]

The default object relationship mapping (ORM) model class that has extensive functionalies.

  • where ModelProtocol is the protocol of Flask SQLAlchemy customized model.