API¶
Anything documented here is part of the public API that Flask-SQLAlchemy provides, unless otherwise indicated. Anything not documented here is considered internal or private and may change at any time.
- class flask_sqlalchemy_lite.SQLAlchemy(app=None, *, require_default_engine=True, engine_options=None, session_options=None)¶
Manage SQLAlchemy engines and sessions for Flask applications.
- Parameters:
app (App | None) – Call
init_app()on this Flask application.require_default_engine (bool) – Whether to raise an error if a
"default"engine is not configured.engine_options (dict[str, t.Any] | None) – Default arguments passed to
sqlalchemy.create_engine()for each configured engine.session_options (dict[str, t.Any] | None) – Arguments to configure
sessionmakerwith.
- init_app(app)¶
Register the extension on an application, creating engines from its
config.- Parameters:
app (App) – The application to register.
- Return type:
None
- get_engine(name='default')¶
Get a specific engine associated with the current application.
The
engineattribute is a shortcut for calling this without an argument to get the default engine.
- property sessionmaker: sessionmaker[Session]¶
The session factory configured for the current application. This can be used to create sessions directly, but they will not be closed automatically at the end of the application context. Use
sessionandget_session()for that.This can also be used to update the session options after
init_app(), by calling itsconfigure()method.
- get_session(name='default')¶
Create a
sqlalchemy.orm.Sessionthat will be closed at the end of the application context. Repeated calls with the same name within the same application context will return the same session.The
sessionattribute is a shortcut for calling this without an argument to get the default session.
- property session: Session¶
The default session for the current application context. It will be closed when the context ends.
- property async_engines: dict[str, AsyncEngine]¶
The async engines associated with the current application.
- get_async_engine(name='default')¶
Get a specific async engine associated with the current application.
The
async_engineattribute is a shortcut for calling this without an argument to get the default engine.- Parameters:
name (str) – The name associated with the engine.
- Return type:
- property async_engine: AsyncEngine¶
The default async engine associated with the current application.
- property async_sessionmaker: async_sessionmaker[AsyncSession]¶
The async session factory configured for the current application. This can be used to create sessions directly, but they will not be closed automatically at the end of the application context. This is the preferred way to use async sessions, by directly creating and managing them.
async_sessionandget_async_session()are available to manage async sessions for the entire application context, but managing them directly with this is preferred.This can also be used to update the session options after
init_app(), by calling itsconfigure()method.
- get_async_session(name='default')¶
Create a
sqlalchemy.ext.asyncio.AsyncSessionthat will be closed at the end of the application context. Repeated calls with the same name within the same application context will return the same session.The
async_sessionattribute is a shortcut for calling this without an argument to get the default async session.Async sessions are not safe to use across concurrent tasks, as they represent unguarded mutable state. Use a separate named session for each task within a single application context, or use
async_sessionmakerto directly control async session lifetime.- Parameters:
name (str) – A unique name for caching the session.
- Return type:
- property async_session: AsyncSession¶
The default async session for the current application context. It will be closed when the context ends.
- get_or_abort(entity, ident, *, options=None, get_kwargs=None, session=None, code=404, abort_kwargs=None)¶
Call
Session.get_oneand return the instance. If not found, callabort()with a 404 error by default.- Parameters:
ident (Any) – The primary key to query.
options (Sequence[ORMOption] | None) – The
optionsargument passed tosession.get.get_kwargs (dict[str, Any] | None) – Arguments passed to
session.get.session (Session | None) – The session to execute the query. Defaults to
session.code (int) – The HTTP error code.
abort_kwargs (dict[str, Any] | None) – Arguments passed to
abort.
- Return type:
M
Added in version 0.2.
- async async_get_or_abort(entity, ident, *, options=None, get_kwargs=None, session=None, code=404, abort_kwargs=None)¶
Async version of
get_or_abort().Added in version 0.2.
- one_or_abort(select: Select, *, scalar: Literal[True] = True, execute_kwargs: dict[str, Any] | None = None, session: Session | None = None, code: int = 404, abort_kwargs: dict[str, Any] | None = None) M¶
- one_or_abort(select: Select, *, scalar: Literal[False], execute_kwargs: dict[str, Any] | None = None, session: Session | None = None, code: int = 404, abort_kwargs: dict[str, Any] | None = None) Row[TP]
Call
Session.executewith the given select statement and return a single result. If zero or multiple results are found, callabort()with a 404 error by default.This is useful instead of
get_or_abort()if you are querying by some other unique key rather than the primary key.executewill return tuples, even if you are selecting a single model class. By default, this function assumes you are querying a single model and callsscalar_one()to return the single instance.- Parameters:
select (Select) – The select statement to execute.
scalar (bool) – Whether to call
scalar_one()on the result to return a scalar instead of a tuple.execute_kwargs (dict[str, Any] | None) – Arguments passed to
session.execute.session (Session | None) – The session to execute the query. Defaults to
session.code (int) – The HTTP error code.
abort_kwargs (dict[str, Any] | None) – Arguments passed to
abort.
- Return type:
M | Row[TP]
Added in version 0.2.
- async async_one_or_abort(select: Select, *, scalar: Literal[True] = True, execute_kwargs: dict[str, Any] | None = None, session: AsyncSession | None = None, code: int = 404, abort_kwargs: dict[str, Any] | None = None) M¶
- async async_one_or_abort(select: Select, *, scalar: Literal[False], execute_kwargs: dict[str, Any] | None = None, session: AsyncSession | None = None, code: int = 404, abort_kwargs: dict[str, Any] | None = None) Row[TP]
Async version of
one_or_abort().Added in version 0.2.
- test_isolation()¶
Context manager to isolate the database during a test. Commits are rolled back when the
withblock exits, and will not be seen by other tests.This patches the SQLAlchemy engine and session to use a single connection, in a transaction that is rolled back when the context exits.
If the code being tested uses async features, use
async_test_isolation()instead. It will isolate both the sync and async operations.When using SQLite, follow the SQLAlchemy docs to fix the driver’s transaction handling.
Added in version 0.2.
- Return type:
Iterator[None]
- async_test_isolation()¶
Async version of
test_isolation()to be used as anasync withblock. It will isolate the sync code as well, so you do not need to usetest_isolationas well.Added in version 0.2.
- Return type:
AsyncIterator[None]