class_docstr: 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.
class_docstr_tok: ["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","."]
code: def __init__(
      self,
      app: Flask | None = None,
      *,
      metadata: sa.MetaData | None = None,
      session_options: dict[str, t.Any] | None = None,
      query_class: type[Query] = Query,
      model_class: _FSA_MCT = Model,  # type: ignore[assignment]
      engine_options: dict[str, t.Any] | None = None,
      add_models_to_shell: bool = True,
      disable_autonaming: bool = False,
  ):
      if session_options is None:
          session_options = {}
  
      self.Query = query_class
      """The default query class used by ``Model.query`` and ``lazy="dynamic"``
          relationships.
  
          .. warning::
              The query interface is considered legacy in SQLAlchemy.
  
          Customize this by passing the ``query_class`` parameter to the extension.
          """
  
      self.session = self._make_scoped_session(session_options)
      """A :class:`sqlalchemy.orm.scoping.scoped_session` that creates instances of
          :class:`.Session` scoped to the current Flask application context. The session
          will be removed, returning the engine connection to the pool, when the
          application context exits.
  
          Customize this by passing ``session_options`` to the extension.
  
          This requires that a Flask application context is active.
  
          .. versionchanged:: 3.0
              The session is scoped to the current app context.
          """
  
      self.metadatas: dict[str | None, sa.MetaData] = {}
      """Map of bind keys to :class:`sqlalchemy.schema.MetaData` instances. The
          ``None`` key refers to the default metadata, and is available as
          :attr:`metadata`.
  
          Customize the default metadata by passing the ``metadata`` parameter to the
          extension. This can be used to set a naming convention. When metadata for
          another bind key is created, it copies the default's naming convention.
  
          .. versionadded:: 3.0
          """
  
      if metadata is not None:
          if len(_get_2x_declarative_bases(model_class)) > 0:
              warnings.warn(
                  "When using SQLAlchemy 2.x style of declarative classes,"
                  " the `metadata` should be an attribute of the base class."
                  "The metadata passed into SQLAlchemy() is ignored.",
                  DeprecationWarning,
                  stacklevel=2,
              )
          else:
              metadata.info["bind_key"] = None
              self.metadatas[None] = metadata
  
      self.Table = self._make_table_class()
      """A :class:`sqlalchemy.schema.Table` class that chooses a metadata
          automatically.
  
          Unlike the base ``Table``, the ``metadata`` argument is not required. If it is
          not given, it is selected based on the ``bind_key`` argument.
  
          :param bind_key: Used to select a different metadata.
          :param args: Arguments passed to the base class. These are typically the table's
              name, columns, and constraints.
          :param kwargs: Arguments passed to the base class.
  
          .. versionchanged:: 3.0
              This is a subclass of SQLAlchemy's ``Table`` rather than a function.
          """
  
      self.Model = self._make_declarative_base(
          model_class, disable_autonaming=disable_autonaming
      )
      """A SQLAlchemy declarative model class. Subclass this to define database
          models.
  
          If a model does not set ``__tablename__``, it will be generated by converting
          the class name from ``CamelCase`` to ``snake_case``. It will not be generated
          if the model looks like it uses single-table inheritance.
  
          If a model or parent class sets ``__bind_key__``, it will use that metadata and
          database engine. Otherwise, it will use the default :attr:`metadata` and
          :attr:`engine`. This is ignored if the model sets ``metadata`` or ``__table__``.
  
          For code using the SQLAlchemy 1.x API, customize this model by subclassing
          :class:`.Model` and passing the ``model_class`` parameter to the extension.
          A fully created declarative model class can be
          passed as well, to use a custom metaclass.
  
          For code using the SQLAlchemy 2.x API, customize this model by subclassing
          :class:`sqlalchemy.orm.DeclarativeBase` or
          :class:`sqlalchemy.orm.DeclarativeBaseNoMeta`
          and passing the ``model_class`` parameter to the extension.
          """
  
      if engine_options is None:
          engine_options = {}
  
      self._engine_options = engine_options
      self._app_engines: WeakKeyDictionary[
          Flask, dict[str | None, sa.engine.Engine]
      ]
      self._app_engines = WeakKeyDictionary()
      self._add_models_to_shell = add_models_to_shell
  
      if app is not None:
          self.init_app(app)
  
imports: from __future__ import annotations
  import os
  import types
  import typing
  import warnings
  from weakref import WeakKeyDictionary
  import sqlalchemy
  import sqlalchemy.event
  import sqlalchemy.exc
  import sqlalchemy.orm
  from flask import abort
  from flask import current_app
  from flask import Flask
  from flask import has_app_context
  from .model import _QueryProperty
  from .model import BindMixin
  from .model import DefaultMeta
  from .model import DefaultMetaNoName
  from .model import Model
  from .model import NameMixin
  from .pagination import Pagination
  from .pagination import SelectPagination
  from .query import Query
  from .session import _app_ctx_id
  from .session import Session
  from .table import _Table
label_desc: Use image node_id 1 to create a new SQLAlchemy object  with example: obj = SQLAlchemy(app)