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,
)
| 1040 | |
| 1041 | |
| 1042 | def declarative_base( |
| 1043 | *, |
| 1044 | metadata: Optional[MetaData] = None, |
| 1045 | mapper: Optional[Callable[..., Mapper[Any]]] = None, |
| 1046 | cls: Type[Any] = object, |
| 1047 | name: str = "Base", |
| 1048 | class_registry: Optional[clsregistry._ClsRegistryType] = None, |
| 1049 | type_annotation_map: Optional[_TypeAnnotationMapType] = None, |
| 1050 | constructor: Callable[..., None] = _declarative_constructor, |
| 1051 | metaclass: Type[Any] = DeclarativeMeta, |
| 1052 | ) -> Any: |
| 1053 | r"""Construct a base class for declarative class definitions. |
| 1054 | |
| 1055 | The new base class will be given a metaclass that produces |
| 1056 | appropriate :class:`~sqlalchemy.schema.Table` objects and makes |
| 1057 | the appropriate :class:`_orm.Mapper` calls based on the |
| 1058 | information provided declaratively in the class and any subclasses |
| 1059 | of the class. |
| 1060 | |
| 1061 | .. versionchanged:: 2.0 Note that the :func:`_orm.declarative_base` |
| 1062 | function is superseded by the new :class:`_orm.DeclarativeBase` class, |
| 1063 | which generates a new "base" class using subclassing, rather than |
| 1064 | return value of a function. This allows an approach that is compatible |
| 1065 | with :pep:`484` typing tools. |
| 1066 | |
| 1067 | The :func:`_orm.declarative_base` function is a shorthand version |
| 1068 | of using the :meth:`_orm.registry.generate_base` |
| 1069 | method. That is, the following:: |
| 1070 | |
| 1071 | from sqlalchemy.orm import declarative_base |
| 1072 | |
| 1073 | Base = declarative_base() |
| 1074 | |
| 1075 | Is equivalent to:: |
| 1076 | |
| 1077 | from sqlalchemy.orm import registry |
| 1078 | |
| 1079 | mapper_registry = registry() |
| 1080 | Base = mapper_registry.generate_base() |
| 1081 | |
| 1082 | See the docstring for :class:`_orm.registry` |
| 1083 | and :meth:`_orm.registry.generate_base` |
| 1084 | for more details. |
| 1085 | |
| 1086 | .. versionchanged:: 1.4 The :func:`_orm.declarative_base` |
| 1087 | function is now a specialization of the more generic |
| 1088 | :class:`_orm.registry` class. The function also moves to the |
| 1089 | ``sqlalchemy.orm`` package from the ``declarative.ext`` package. |
| 1090 | |
| 1091 | |
| 1092 | :param metadata: |
| 1093 | An optional :class:`~sqlalchemy.schema.MetaData` instance. All |
| 1094 | :class:`~sqlalchemy.schema.Table` objects implicitly declared by |
| 1095 | subclasses of the base will share this MetaData. A MetaData instance |
| 1096 | will be created if none is provided. The |
| 1097 | :class:`~sqlalchemy.schema.MetaData` instance will be available via the |
| 1098 | ``metadata`` attribute of the generated declarative base class. |
| 1099 |