class registry: (source)
Generalized registry for mapping classes. The :class:`_orm.registry` serves as the basis for maintaining a collection of mappings, and provides configurational hooks used to map classes. The three general kinds of mappings supported are Declarative Base, Declarative Decorator, and Imperative Mapping. All of these mapping styles may be used interchangeably: * :meth:`_orm.registry.generate_base` returns a new declarative base class, and is the underlying implementation of the :func:`_orm.declarative_base` function. * :meth:`_orm.registry.mapped` provides a class decorator that will apply declarative mapping to a class without the use of a declarative base class. * :meth:`_orm.registry.map_imperatively` will produce a :class:`_orm.Mapper` for a class without scanning the class for declarative class attributes. This method suits the use case historically provided by the ``sqlalchemy.orm.mapper()`` classical mapping function, which is removed as of SQLAlchemy 2.0. .. versionadded:: 1.4 .. seealso:: :ref:`orm_mapping_classes_toplevel` - overview of class mapping styles.
Method | __init__ |
Construct a new :class:`_orm.registry` |
Method | as |
Class decorator which will invoke :meth:`_orm.registry.generate_base` for a given base class. |
Method | configure |
Configure all as-yet unconfigured mappers in this :class:`_orm.registry`. |
Method | dispose |
Dispose of all mappers in this :class:`_orm.registry`. |
Method | generate |
Generate a declarative base class. |
Method | map |
Map a class declaratively. |
Method | map |
Map a class imperatively. |
Method | mapped |
Class decorator that will apply the Declarative mapping process to a given class. |
Method | mapped |
Class decorator that will apply the Declarative mapping process to a given class, and additionally convert the class to be a Python dataclass. |
Method | update |
update the :paramref:`_orm.registry.type_annotation_map` with new values. |
Instance Variable | constructor |
Undocumented |
Instance Variable | metadata |
Undocumented |
Instance Variable | type |
Undocumented |
Property | mappers |
read only collection of all :class:`_orm.Mapper` objects. |
Class Method | _recurse |
Undocumented |
Class Method | _recurse |
Undocumented |
Method | _add |
Undocumented |
Method | _add |
Undocumented |
Method | _dispose |
Undocumented |
Method | _dispose |
Undocumented |
Method | _flag |
Undocumented |
Method | _mappers |
Undocumented |
Method | _resolve |
Undocumented |
Method | _set |
Undocumented |
Instance Variable | _class |
Undocumented |
Instance Variable | _dependencies |
Undocumented |
Instance Variable | _dependents |
Undocumented |
Instance Variable | _managers |
Undocumented |
Instance Variable | _new |
Undocumented |
Instance Variable | _non |
Undocumented |
Optional[ MetaData]
= None, class_registry: Optional[ clsregistry._ClsRegistryType]
= None, type_annotation_map: Optional[ _TypeAnnotationMapType]
= None, constructor: Callable[ ..., None]
= _declarative_constructor):
(source)
¶
Construct a new :class:`_orm.registry` :param metadata: An optional :class:`_schema.MetaData` instance. All :class:`_schema.Table` objects generated using declarative table mapping will make use of this :class:`_schema.MetaData` collection. If this argument is left at its default of ``None``, a blank :class:`_schema.MetaData` collection is created. :param constructor: Specify the implementation for the ``__init__`` function on a mapped class that has no ``__init__`` of its own. Defaults to an implementation that assigns \**kwargs for declared fields and relationships to an instance. If ``None`` is supplied, no __init__ will be provided and construction will fall back to cls.__init__ by way of the normal Python semantics. :param class_registry: optional dictionary that will serve as the registry of class names-> mapped classes when string names are used to identify classes inside of :func:`_orm.relationship` and others. Allows two or more declarative base classes to share the same registry of class names for simplified inter-base relationships. :param type_annotation_map: optional dictionary of Python types to SQLAlchemy :class:`_types.TypeEngine` classes or instances. The provided dict will update the default type mapping. This is used exclusively by the :class:`_orm.MappedColumn` construct to produce column types based on annotations within the :class:`_orm.Mapped` type. .. versionadded:: 2.0 .. seealso:: :ref:`orm_declarative_mapped_column_type_map`
Class decorator which will invoke :meth:`_orm.registry.generate_base` for a given base class. E.g.:: from sqlalchemy.orm import registry mapper_registry = registry() @mapper_registry.as_declarative_base() class Base: @declared_attr def __tablename__(cls): return cls.__name__.lower() id = Column(Integer, primary_key=True) class MyMappedClass(Base): # ... All keyword arguments passed to :meth:`_orm.registry.as_declarative_base` are passed along to :meth:`_orm.registry.generate_base`.
Configure all as-yet unconfigured mappers in this :class:`_orm.registry`. The configure step is used to reconcile and initialize the :func:`_orm.relationship` linkages between mapped classes, as well as to invoke configuration events such as the :meth:`_orm.MapperEvents.before_configured` and :meth:`_orm.MapperEvents.after_configured`, which may be used by ORM extensions or user-defined extension hooks. If one or more mappers in this registry contain :func:`_orm.relationship` constructs that refer to mapped classes in other registries, this registry is said to be *dependent* on those registries. In order to configure those dependent registries automatically, the :paramref:`_orm.registry.configure.cascade` flag should be set to ``True``. Otherwise, if they are not configured, an exception will be raised. The rationale behind this behavior is to allow an application to programmatically invoke configuration of registries while controlling whether or not the process implicitly reaches other registries. As an alternative to invoking :meth:`_orm.registry.configure`, the ORM function :func:`_orm.configure_mappers` function may be used to ensure configuration is complete for all :class:`_orm.registry` objects in memory. This is generally simpler to use and also predates the usage of :class:`_orm.registry` objects overall. However, this function will impact all mappings throughout the running Python process and may be more memory/time consuming for an application that has many registries in use for different purposes that may not be needed immediately. .. seealso:: :func:`_orm.configure_mappers` .. versionadded:: 1.4.0b2
Dispose of all mappers in this :class:`_orm.registry`. After invocation, all the classes that were mapped within this registry will no longer have class instrumentation associated with them. This method is the per-:class:`_orm.registry` analogue to the application-wide :func:`_orm.clear_mappers` function. If this registry contains mappers that are dependencies of other registries, typically via :func:`_orm.relationship` links, then those registries must be disposed as well. When such registries exist in relation to this one, their :meth:`_orm.registry.dispose` method will also be called, if the :paramref:`_orm.registry.dispose.cascade` flag is set to ``True``; otherwise, an error is raised if those registries were not already disposed. .. versionadded:: 1.4.0b2 .. seealso:: :func:`_orm.clear_mappers`
Optional[ Callable[ ..., Mapper[ Any]]]
= None, cls: Type[ Any]
= object, name: str
= 'Base', metaclass: Type[ Any]
= DeclarativeMeta) -> Any
:
(source)
¶
Generate a declarative base class. Classes that inherit from the returned class object will be automatically mapped using declarative mapping. E.g.:: from sqlalchemy.orm import registry mapper_registry = registry() Base = mapper_registry.generate_base() class MyClass(Base): __tablename__ = "my_table" id = Column(Integer, primary_key=True) The above dynamically generated class is equivalent to the non-dynamic example below:: from sqlalchemy.orm import registry from sqlalchemy.orm.decl_api import DeclarativeMeta mapper_registry = registry() class Base(metaclass=DeclarativeMeta): __abstract__ = True registry = mapper_registry metadata = mapper_registry.metadata __init__ = mapper_registry.constructor .. versionchanged:: 2.0 Note that the :meth:`_orm.registry.generate_base` method is superseded by the new :class:`_orm.DeclarativeBase` class, which generates a new "base" class using subclassing, rather than return value of a function. This allows an approach that is compatible with :pep:`484` typing tools. The :meth:`_orm.registry.generate_base` method provides the implementation for the :func:`_orm.declarative_base` function, which creates the :class:`_orm.registry` and base class all at once. See the section :ref:`orm_declarative_mapping` for background and examples. :param mapper: An optional callable, defaults to :class:`_orm.Mapper`. This function is used to generate new :class:`_orm.Mapper` objects. :param cls: Defaults to :class:`object`. A type to use as the base for the generated declarative base class. May be a class or tuple of classes. :param name: Defaults to ``Base``. The display name for the generated class. Customizing this is not required, but can improve clarity in tracebacks and debugging. :param metaclass: Defaults to :class:`.DeclarativeMeta`. A metaclass or __metaclass__ compatible callable to use as the meta type of the generated declarative base class. .. seealso:: :ref:`orm_declarative_mapping` :func:`_orm.declarative_base`
Map a class declaratively. In this form of mapping, the class is scanned for mapping information, including for columns to be associated with a table, and/or an actual table object. Returns the :class:`_orm.Mapper` object. E.g.:: from sqlalchemy.orm import registry mapper_registry = registry() class Foo: __tablename__ = 'some_table' id = Column(Integer, primary_key=True) name = Column(String) mapper = mapper_registry.map_declaratively(Foo) This function is more conveniently invoked indirectly via either the :meth:`_orm.registry.mapped` class decorator or by subclassing a declarative metaclass generated from :meth:`_orm.registry.generate_base`. See the section :ref:`orm_declarative_mapping` for complete details and examples. :param cls: class to be mapped. :return: a :class:`_orm.Mapper` object. .. seealso:: :ref:`orm_declarative_mapping` :meth:`_orm.registry.mapped` - more common decorator interface to this function. :meth:`_orm.registry.map_imperatively`
Type[ _O]
, local_table: Optional[ FromClause]
= None, **kw: Any
) -> Mapper[ _O]
:
(source)
¶
Map a class imperatively. In this form of mapping, the class is not scanned for any mapping information. Instead, all mapping constructs are passed as arguments. This method is intended to be fully equivalent to the now-removed SQLAlchemy ``mapper()`` function, except that it's in terms of a particular registry. E.g.:: from sqlalchemy.orm import registry mapper_registry = registry() my_table = Table( "my_table", mapper_registry.metadata, Column('id', Integer, primary_key=True) ) class MyClass: pass mapper_registry.map_imperatively(MyClass, my_table) See the section :ref:`orm_imperative_mapping` for complete background and usage examples. :param class\_: The class to be mapped. Corresponds to the :paramref:`_orm.Mapper.class_` parameter. :param local_table: the :class:`_schema.Table` or other :class:`_sql.FromClause` object that is the subject of the mapping. Corresponds to the :paramref:`_orm.Mapper.local_table` parameter. :param \**kw: all other keyword arguments are passed to the :class:`_orm.Mapper` constructor directly. .. seealso:: :ref:`orm_imperative_mapping` :ref:`orm_declarative_mapping`
Class decorator that will apply the Declarative mapping process to a given class. E.g.:: from sqlalchemy.orm import registry mapper_registry = registry() @mapper_registry.mapped class Foo: __tablename__ = 'some_table' id = Column(Integer, primary_key=True) name = Column(String) See the section :ref:`orm_declarative_mapping` for complete details and examples. :param cls: class to be mapped. :return: the class that was passed. .. seealso:: :ref:`orm_declarative_mapping` :meth:`_orm.registry.generate_base` - generates a base class that will apply Declarative mapping to subclasses automatically using a Python metaclass. .. seealso:: :meth:`_orm.registry.mapped_as_dataclass`
@overload
@overload
Literal[ None]
= ..., *, init: Union[ _NoArg, bool]
= ..., repr: Union[ _NoArg, bool]
= ..., eq: Union[ _NoArg, bool]
= ..., order: Union[ _NoArg, bool]
= ..., unsafe_hash: Union[ _NoArg, bool]
= ..., match_args: Union[ _NoArg, bool]
= ..., kw_only: Union[ _NoArg, bool]
= ..., dataclass_callable: Union[ _NoArg, Callable[ ..., Type[ Any]]]
= ...) -> Callable[ [ Type[ _O]], Type[ _O]]
:Class decorator that will apply the Declarative mapping process to a given class, and additionally convert the class to be a Python dataclass. .. seealso:: :ref:`orm_declarative_native_dataclasses` - complete background on SQLAlchemy native dataclass mapping .. versionadded:: 2.0
def _recurse_with_dependencies(cls, registries:
Set[ RegistryType]
) -> Iterator[ RegistryType]
:
(source)
¶
Undocumented
def _recurse_with_dependents(cls, registries:
Set[ RegistryType]
) -> Iterator[ RegistryType]
:
(source)
¶
Undocumented
_MatchedOnType
) -> Optional[ sqltypes.TypeEngine[ Any]]
:
(source)
¶
Undocumented