flask_more_smorest.sqla.db
- flask_more_smorest.sqla.db = <SQLAlchemy>
Integrates SQLAlchemy with Flask. This handles setting up one or more engines, associating tables and models with specific engines, and cleaning up connections and sessions after each request.
Only the engine configuration is specific to each application, other things like the model, table, metadata, and session are shared for all applications using that extension instance. Call
init_app()to configure the extension on an application.After creating the extension, create model classes by subclassing
Model, and table classes withTable. These can be accessed beforeinit_app()is called, making it possible to define the models separately from the application.Accessing
sessionandenginerequires an active Flask application context. This includes methods likecreate_all()which use the engine.This class also provides access to names in SQLAlchemy’s
sqlalchemyandsqlalchemy.ormmodules. For example, you can usedb.Columnanddb.relationshipinstead of importingsqlalchemy.Columnandsqlalchemy.orm.relationship. This can be convenient when defining models.- Parameters:
app – Call
init_app()on this Flask application now.metadata – Use this as the default
sqlalchemy.schema.MetaData. Useful for setting a naming convention.session_options – Arguments used by
sessionto create each session instance. Ascopefunckey will be passed to the scoped session, not the session instance. Seesqlalchemy.orm.sessionmakerfor a list of arguments.query_class – Use this as the default query class for models and dynamic relationships. The query interface is considered legacy in SQLAlchemy.
model_class – Use this as the model base class when creating the declarative model class
Model. Can also be a fully created declarative model class for further customization.engine_options – Default arguments used when creating every engine. These are lower precedence than application config. See
sqlalchemy.create_engine()for a list of arguments.add_models_to_shell – Add the
dbinstance and all model classes toflask shell.
Changed in version 3.1.0: The
metadataparameter can still be used with SQLAlchemy 1.x classes, but is ignored when using SQLAlchemy 2.x style of declarative classes. Instead, specify metadata on your Base class.Changed in version 3.1.0: Added the
disable_autonamingparameter.Changed in version 3.1.0: Changed
model_classparameter to accepta SQLAlchemy 2.x declarative base subclass.Changed in version 3.0: An active Flask application context is always required to access
sessionandengine.Changed in version 3.0: Separate
metadataare used for each bind key.Changed in version 3.0: The
engine_optionsparameter is applied as defaults before per-engine configuration.Changed in version 3.0: The session class can be customized in
session_options.Changed in version 3.0: Added the
add_models_to_shellparameter.Changed in version 3.0: Engines are created when calling
init_apprather than the first time they are accessed.Changed in version 3.0: All parameters except
appare keyword-only.Changed in version 3.0: The extension instance is stored directly as
app.extensions["sqlalchemy"].Changed in version 3.0: Setup methods are renamed with a leading underscore. They are considered internal interfaces which may change at any time.
Changed in version 3.0: Removed the
use_native_unicodeparameter and config.Changed in version 2.4: Added the
engine_optionsparameter.Changed in version 2.1: Added the
metadata,query_class, andmodel_classparameters.Changed in version 2.1: Use the same query class across
session,Model.queryandQuery.Changed in version 0.16:
scopefuncis accepted insession_options.Changed in version 0.10: Added the
session_optionsparameter.