Skip to main content

get_flask_sqlalchemy

FunctionSource

db: flask_sqlalchemy.SQLAlchemy = get_flask_sqlalchemy(
model_class: type[sa_orm.DeclarativeBase],
app: Flask | None = None,
engine_options: Mapping[str, Any] | None = None,
session_options: Mapping[str, Any] | None = None,
)

Get the Flask SQLAlchemy DB instance in the compatible mode.

This method will attempt to get the flask_sqlalchemy.SQLAlchemy DB instance. If the attempt fails (package is not installed), will fallback to use flask_sqlalchemy_lite.SQLAlchemy to imitate the interfaces of flask_sqlalchemy.SQLAlchemy.

Aliases

This function can be acquired by

import flask_sqlalchemy_compat as fsc


fsc.get_flask_sqlalchemy
fsc.auto.get_flask_sqlalchemy

Arguments

Requires

ArgumentTypeRequired
Description
model_classtype[sa.orm.DeclarativeBase]The base model type applied to the whole database. If flask_sqlalchemy is available, this type will be used as model_class argument for creating the db-specific base model type. However, if the db instance fallback to the version provided by flask_sqlalchemy_lite, this type will be modified inplace to imitate the behavior of flask_sqlalchemy.SQLAlchemy().Model.
appFlask | NoneCall init_app on this Flask application. If not specified, will not call init_app.
engine_optionsMapping[str, Any] | NoneDefault arguments passed to sqlalchemy.create_engine for each configured engine.
session_optionsMapping[str, Any] | NoneArguments to configure sessionmaker with.

Returns

ArgumentType
Description
dbflask_sqlalchemy.SQLAlchemy

The SQLAlchemy extension instance. It will be an instance of flask_sqlalchemy.SQLAlchemy if the package is available.

If the package is not available, will attempt to return a SQLAlchemyProxy[flask_sqlalchemy_lite.SQLAlchemy, _ModelLite] instance. However, this returned value is still notated by flask_sqlalchemy.SQLAlchemy, which indicates that users should use flask_sqlalchemy to develop their codes, while making this returned value as a falling back option.