Participer au site avec un Tip
Rechercher
 

Améliorations / Corrections

Vous avez des améliorations (ou des corrections) à proposer pour ce document : je vous remerçie par avance de m'en faire part, cela m'aide à améliorer le site.

Emplacement :

Description des améliorations :

Vous êtes un professionnel et vous avez besoin d'une formation ? Calcul scientifique
avec Python
Voir le programme détaillé
Module « flask_sqlalchemy »

Classe « SQLAlchemy »

Informations générales

Héritage

builtins.object
    SQLAlchemy

Définition

class SQLAlchemy(builtins.object):

help(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 :meth:`init_app` to configure the extension on an
application.

After creating the extension, create model classes by subclassing :attr:`Model`, and
table classes with :attr:`Table`. These can be accessed before :meth:`init_app` is
called, making it possible to define the models separately from the application.

Accessing :attr:`session` and :attr:`engine` requires an active Flask application
context. This includes methods like :meth:`create_all` which use the engine.

This class also provides access to names in SQLAlchemy's ``sqlalchemy`` and
``sqlalchemy.orm`` modules. For example, you can use ``db.Column`` and
``db.relationship`` instead of importing ``sqlalchemy.Column`` and
``sqlalchemy.orm.relationship``. This can be convenient when defining models.

:param app: Call :meth:`init_app` on this Flask application now.
:param metadata: Use this as the default :class:`sqlalchemy.schema.MetaData`. Useful
    for setting a naming convention.
:param session_options: Arguments used by :attr:`session` to create each session
    instance. A ``scopefunc`` key will be passed to the scoped session, not the
    session instance. See :class:`sqlalchemy.orm.sessionmaker` for a list of
    arguments.
:param query_class: Use this as the default query class for models and dynamic
    relationships. The query interface is considered legacy in SQLAlchemy.
:param model_class: Use this as the model base class when creating the declarative
    model class :attr:`Model`. Can also be a fully created declarative model class
    for further customization.
:param engine_options: Default arguments used when creating every engine. These are
    lower precedence than application config. See :func:`sqlalchemy.create_engine`
    for a list of arguments.
:param add_models_to_shell: Add the ``db`` instance and all model classes to
    ``flask shell``.

.. versionchanged:: 3.1.0
    The ``metadata`` parameter 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.

.. versionchanged:: 3.1.0
    Added the ``disable_autonaming`` parameter.

.. versionchanged:: 3.1.0
    Changed ``model_class`` parameter to accepta SQLAlchemy 2.x
    declarative base subclass.

.. versionchanged:: 3.0
    An active Flask application context is always required to access ``session`` and
    ``engine``.

.. versionchanged:: 3.0
    Separate ``metadata`` are used for each bind key.

.. versionchanged:: 3.0
    The ``engine_options`` parameter is applied as defaults before per-engine
    configuration.

.. versionchanged:: 3.0
    The session class can be customized in ``session_options``.

.. versionchanged:: 3.0
    Added the ``add_models_to_shell`` parameter.

.. versionchanged:: 3.0
    Engines are created when calling ``init_app`` rather than the first time they
    are accessed.

.. versionchanged:: 3.0
    All parameters except ``app`` are keyword-only.

.. versionchanged:: 3.0
    The extension instance is stored directly as ``app.extensions["sqlalchemy"]``.

.. versionchanged:: 3.0
    Setup methods are renamed with a leading underscore. They are considered
    internal interfaces which may change at any time.

.. versionchanged:: 3.0
    Removed the ``use_native_unicode`` parameter and config.

.. versionchanged:: 2.4
    Added the ``engine_options`` parameter.

.. versionchanged:: 2.1
    Added the ``metadata``, ``query_class``, and ``model_class`` parameters.

.. versionchanged:: 2.1
    Use the same query class across ``session``, ``Model.query`` and
    ``Query``.

.. versionchanged:: 0.16
    ``scopefunc`` is accepted in ``session_options``.

.. versionchanged:: 0.10
    Added the ``session_options`` parameter.

Constructeur(s)

Signature du constructeur Description
__init__(self, app: 'Flask | None' = None, *, metadata: 'sa.MetaData | None' = None, session_options: 'dict[str, t.Any] | None' = None, query_class: 'type[Query]' = <class 'flask_sqlalchemy.query.Query'>, model_class: '_FSA_MCT' = <class 'flask_sqlalchemy.model.Model'>, engine_options: 'dict[str, t.Any] | None' = None, add_models_to_shell: 'bool' = True, disable_autonaming: 'bool' = False)

Liste des propriétés

Nom de la propriétéDescription
engineThe default :class:`~sqlalchemy.engine.Engine` for the current application, [extrait de engine.__doc__]
enginesMap of bind keys to :class:`sqlalchemy.engine.Engine` instances for current [extrait de engines.__doc__]
metadataThe default metadata used by :attr:`Model` and :attr:`Table` if no bind key [extrait de metadata.__doc__]

Liste des opérateurs

Opérateurs hérités de la classe object

__eq__, __ge__, __gt__, __le__, __lt__, __ne__

Liste des méthodes

Toutes les méthodes Méthodes d'instance Méthodes statiques Méthodes dépréciées
Signature de la méthodeDescription
__getattr__(self, name: 'str') -> 't.Any'
__repr__(self) -> 'str'
create_all(self, bind_key: 'str | None | list[str | None]' = '__all__') -> 'None' Create tables that do not exist in the database by calling [extrait de create_all.__doc__]
drop_all(self, bind_key: 'str | None | list[str | None]' = '__all__') -> 'None' Drop tables by calling ``metadata.drop_all()`` for all or some bind keys. [extrait de drop_all.__doc__]
dynamic_loader(self, argument: 't.Any', **kwargs: 't.Any') -> 'sa_orm.RelationshipProperty[t.Any]' A :func:`sqlalchemy.orm.dynamic_loader` that applies this extension's [extrait de dynamic_loader.__doc__]
first_or_404(self, statement: 'sa.sql.Select[t.Any]', *, description: 'str | None' = None) -> 't.Any' Like :meth:`Result.scalar() <sqlalchemy.engine.Result.scalar>`, but aborts [extrait de first_or_404.__doc__]
get_engine(self, bind_key: 'str | None' = None, **kwargs: 't.Any') -> 'sa.engine.Engine' Get the engine for the given bind key for the current application. [extrait de get_engine.__doc__]
get_or_404(self, entity: 'type[_O]', ident: 't.Any', *, description: 'str | None' = None, **kwargs: 't.Any') -> '_O' Like :meth:`session.get() <sqlalchemy.orm.Session.get>` but aborts with a [extrait de get_or_404.__doc__]
init_app(self, app: 'Flask') -> 'None' Initialize a Flask application for use with this extension instance. This [extrait de init_app.__doc__]
one_or_404(self, statement: 'sa.sql.Select[t.Any]', *, description: 'str | None' = None) -> 't.Any' Like :meth:`Result.scalar_one() <sqlalchemy.engine.Result.scalar_one>`, [extrait de one_or_404.__doc__]
paginate(self, select: 'sa.sql.Select[t.Any]', *, page: 'int | None' = None, per_page: 'int | None' = None, max_per_page: 'int | None' = None, error_out: 'bool' = True, count: 'bool' = True) -> 'Pagination' Apply an offset and limit to a select statment based on the current page and [extrait de paginate.__doc__]
reflect(self, bind_key: 'str | None | list[str | None]' = '__all__') -> 'None' Load table definitions from the database by calling ``metadata.reflect()`` [extrait de reflect.__doc__]
relationship(self, *args: 't.Any', **kwargs: 't.Any') -> 'sa_orm.RelationshipProperty[t.Any]' A :func:`sqlalchemy.orm.relationship` that applies this extension's [extrait de relationship.__doc__]

Méthodes héritées de la classe object

__delattr__, __dir__, __format__, __getattribute__, __getstate__, __hash__, __init_subclass__, __reduce__, __reduce_ex__, __setattr__, __sizeof__, __str__, __subclasshook__

Vous êtes un professionnel et vous avez besoin d'une formation ? Calcul scientifique
avec Python
Voir le programme détaillé