class documentation

class AutomapBase: (source)

View In Hierarchy

Base class for an "automap" schema. The :class:`.AutomapBase` class can be compared to the "declarative base" class that is produced by the :func:`.declarative.declarative_base` function. In practice, the :class:`.AutomapBase` class is always used as a mixin along with an actual declarative base. A new subclassable :class:`.AutomapBase` is typically instantiated using the :func:`.automap_base` function. .. seealso:: :ref:`automap_toplevel`

Class Method prepare Extract mapped classes and relationships from the :class:`_schema.MetaData` and perform mappings.
Class Variable __abstract__ Undocumented
Class Variable by_module An instance of :class:`.util.Properties` containing a hierarchal structure of dot-separated module names linked to classes.
Class Variable classes An instance of :class:`.util.Properties` containing classes.
Class Variable metadata Refers to the :class:`_schema.MetaData` collection that will be used for new :class:`_schema.Table` objects.
Class Method _sa_raise_deferred_config Undocumented
Class Variable _sa_automapbase_bookkeeping Undocumented
Class Variable _sa_decl_prepare Indicate that the mapping of classes should be deferred.
@classmethod
@util.deprecated_params(engine=('2.0', 'The :paramref:`_automap.AutomapBase.prepare.engine` parameter is deprecated and will be removed in a future release. Please use the :paramref:`_automap.AutomapBase.prepare.autoload_with` parameter.'), reflect=('2.0', 'The :paramref:`_automap.AutomapBase.prepare.reflect` parameter is deprecated and will be removed in a future release. Reflection is enabled when :paramref:`_automap.AutomapBase.prepare.autoload_with` is passed.'))
def prepare(cls: Type[AutomapBase], autoload_with: Optional[Engine] = None, engine: Optional[Any] = None, reflect: bool = False, schema: Optional[str] = None, classname_for_table: Optional[PythonNameForTableType] = None, modulename_for_table: Optional[PythonNameForTableType] = None, collection_class: Optional[Any] = None, name_for_scalar_relationship: Optional[NameForScalarRelationshipType] = None, name_for_collection_relationship: Optional[NameForCollectionRelationshipType] = None, generate_relationship: Optional[GenerateRelationshipType] = None, reflection_options: Union[Dict[_KT, _VT], immutabledict[_KT, _VT]] = util.EMPTY_DICT): (source)

Extract mapped classes and relationships from the :class:`_schema.MetaData` and perform mappings. For full documentation and examples see :ref:`automap_basic_use`. :param autoload_with: an :class:`_engine.Engine` or :class:`_engine.Connection` with which to perform schema reflection; when specified, the :meth:`_schema.MetaData.reflect` method will be invoked within the scope of this method. :param engine: legacy; use :paramref:`.AutomapBase.autoload_with`. Used to indicate the :class:`_engine.Engine` or :class:`_engine.Connection` with which to reflect tables with, if :paramref:`.AutomapBase.reflect` is True. :param reflect: legacy; use :paramref:`.AutomapBase.autoload_with`. Indicates that :meth:`_schema.MetaData.reflect` should be invoked. :param classname_for_table: callable function which will be used to produce new class names, given a table name. Defaults to :func:`.classname_for_table`. :param modulename_for_table: callable function which will be used to produce the effective ``__module__`` for an internally generated class, to allow for multiple classes of the same name in a single automap base which would be in different "modules". Defaults to ``None``, which will indicate that ``__module__`` will not be set explicitly; the Python runtime will use the value ``sqlalchemy.ext.automap`` for these classes. When assigning ``__module__`` to generated classes, they can be accessed based on dot-separated module names using the :attr:`.AutomapBase.by_module` collection. Classes that have an explicit ``__module_`` assigned using this hook do **not** get placed into the :attr:`.AutomapBase.classes` collection, only into :attr:`.AutomapBase.by_module`. .. versionadded:: 2.0 .. seealso:: :ref:`automap_by_module` :param name_for_scalar_relationship: callable function which will be used to produce relationship names for scalar relationships. Defaults to :func:`.name_for_scalar_relationship`. :param name_for_collection_relationship: callable function which will be used to produce relationship names for collection-oriented relationships. Defaults to :func:`.name_for_collection_relationship`. :param generate_relationship: callable function which will be used to actually generate :func:`_orm.relationship` and :func:`.backref` constructs. Defaults to :func:`.generate_relationship`. :param collection_class: the Python collection class that will be used when a new :func:`_orm.relationship` object is created that represents a collection. Defaults to ``list``. :param schema: Schema name to reflect when reflecting tables using the :paramref:`.AutomapBase.prepare.autoload_with` parameter. The name is passed to the :paramref:`_schema.MetaData.reflect.schema` parameter of :meth:`_schema.MetaData.reflect`. When omitted, the default schema in use by the database connection is used. .. note:: The :paramref:`.AutomapBase.prepare.schema` parameter supports reflection of a single schema at a time. In order to include tables from many schemas, use multiple calls to :meth:`.AutomapBase.prepare`. For an overview of multiple-schema automap including the use of additional naming conventions to resolve table name conflicts, see the section :ref:`automap_by_module`. .. versionadded:: 2.0 :meth:`.AutomapBase.prepare` supports being directly invoked any number of times, keeping track of tables that have already been processed to avoid processing them a second time. :param reflection_options: When present, this dictionary of options will be passed to :meth:`_schema.MetaData.reflect` to supply general reflection-specific options like ``only`` and/or dialect-specific options like ``oracle_resolve_synonyms``. .. versionadded:: 1.4

__abstract__: bool = (source)

Undocumented

An instance of :class:`.util.Properties` containing a hierarchal structure of dot-separated module names linked to classes. This collection is an alternative to the :attr:`.AutomapBase.classes` collection that is useful when making use of the :paramref:`.AutomapBase.prepare.modulename_for_table` parameter, which will apply distinct ``__module__`` attributes to generated classes. The default ``__module__`` an automap-generated class is ``sqlalchemy.ext.automap``; to access this namespace using :attr:`.AutomapBase.by_module` looks like:: User = Base.by_module.sqlalchemy.ext.automap.User If a class had a ``__module__`` of ``mymodule.account``, accessing this namespace looks like:: MyClass = Base.by_module.mymodule.account.MyClass .. versionadded:: 2.0 .. seealso:: :ref:`automap_by_module`

classes: ClassVar[Properties[Type[Any]]] = (source)

An instance of :class:`.util.Properties` containing classes. This object behaves much like the ``.c`` collection on a table. Classes are present under the name they were given, e.g.:: Base = automap_base() Base.prepare(autoload_with=some_engine) User, Address = Base.classes.User, Base.classes.Address

Refers to the :class:`_schema.MetaData` collection that will be used for new :class:`_schema.Table` objects. .. seealso:: :ref:`orm_declarative_metadata`

@classmethod
def _sa_raise_deferred_config(cls) -> NoReturn: (source)

Undocumented

_sa_automapbase_bookkeeping: ClassVar[_Bookkeeping] = (source)

Undocumented

_sa_decl_prepare: bool = (source)

Indicate that the mapping of classes should be deferred. The presence of this attribute name indicates to declarative that the call to mapper() should not occur immediately; instead, information about the table and attributes to be mapped are gathered into an internal structure called _DeferredMapperConfig. These objects can be collected later using classes_for_base(), additional mapping decisions can be made, and then the map() method will actually apply the mapping. The only real reason this deferral of the whole thing is needed is to support primary key columns that aren't reflected yet when the class is declared; everything else can theoretically be added to the mapper later. However, the _DeferredMapperConfig is a nice interface in any case which exists at that not usually exposed point at which declarative has the class and the Table but hasn't called mapper() yet.