r"""Construct a base class for declarative class definitions. The new base class will be given a metaclass that produces appropriate :class:`~sqlalchemy.schema.Table` objects and makes the appropriate :class:`_orm.Mapper` calls based on the information provided declaratively in the
(
*,
metadata: Optional[MetaData] = None,
mapper: Optional[Callable[..., Mapper[Any]]] = None,
cls: Type[Any] = object,
name: str = "Base",
class_registry: Optional[clsregistry._ClsRegistryType] = None,
type_annotation_map: Optional[_TypeAnnotationMapType] = None,
constructor: Callable[..., None] = _declarative_constructor,
metaclass: Type[Any] = DeclarativeMeta,
)
| 992 | |
| 993 | |
| 994 | def declarative_base( |
| 995 | *, |
| 996 | metadata: Optional[MetaData] = None, |
| 997 | mapper: Optional[Callable[..., Mapper[Any]]] = None, |
| 998 | cls: Type[Any] = object, |
| 999 | name: str = "Base", |
| 1000 | class_registry: Optional[clsregistry._ClsRegistryType] = None, |
| 1001 | type_annotation_map: Optional[_TypeAnnotationMapType] = None, |
| 1002 | constructor: Callable[..., None] = _declarative_constructor, |
| 1003 | metaclass: Type[Any] = DeclarativeMeta, |
| 1004 | ) -> Any: |
| 1005 | r"""Construct a base class for declarative class definitions. |
| 1006 | |
| 1007 | The new base class will be given a metaclass that produces |
| 1008 | appropriate :class:`~sqlalchemy.schema.Table` objects and makes |
| 1009 | the appropriate :class:`_orm.Mapper` calls based on the |
| 1010 | information provided declaratively in the class and any subclasses |
| 1011 | of the class. |
| 1012 | |
| 1013 | .. versionchanged:: 2.0 Note that the :func:`_orm.declarative_base` |
| 1014 | function is superseded by the new :class:`_orm.DeclarativeBase` class, |
| 1015 | which generates a new "base" class using subclassing, rather than |
| 1016 | return value of a function. This allows an approach that is compatible |
| 1017 | with :pep:`484` typing tools. |
| 1018 | |
| 1019 | The :func:`_orm.declarative_base` function is a shorthand version |
| 1020 | of using the :meth:`_orm.registry.generate_base` |
| 1021 | method. That is, the following:: |
| 1022 | |
| 1023 | from sqlalchemy.orm import declarative_base |
| 1024 | |
| 1025 | Base = declarative_base() |
| 1026 | |
| 1027 | Is equivalent to:: |
| 1028 | |
| 1029 | from sqlalchemy.orm import registry |
| 1030 | |
| 1031 | mapper_registry = registry() |
| 1032 | Base = mapper_registry.generate_base() |
| 1033 | |
| 1034 | See the docstring for :class:`_orm.registry` |
| 1035 | and :meth:`_orm.registry.generate_base` |
| 1036 | for more details. |
| 1037 | |
| 1038 | .. versionchanged:: 1.4 The :func:`_orm.declarative_base` |
| 1039 | function is now a specialization of the more generic |
| 1040 | :class:`_orm.registry` class. The function also moves to the |
| 1041 | ``sqlalchemy.orm`` package from the ``declarative.ext`` package. |
| 1042 | |
| 1043 | |
| 1044 | :param metadata: |
| 1045 | An optional :class:`~sqlalchemy.schema.MetaData` instance. All |
| 1046 | :class:`~sqlalchemy.schema.Table` objects implicitly declared by |
| 1047 | subclasses of the base will share this MetaData. A MetaData instance |
| 1048 | will be created if none is provided. The |
| 1049 | :class:`~sqlalchemy.schema.MetaData` instance will be available via the |
| 1050 | ``metadata`` attribute of the generated declarative base class. |
| 1051 |