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_regist
(
self,
mapper: Optional[Callable[..., Mapper[Any]]] = None,
cls: Type[Any] = object,
name: str = "Base",
metaclass: Type[Any] = DeclarativeMeta,
)
| 1552 | instrumentation._instrumentation_factory.unregister(class_) |
| 1553 | |
| 1554 | def generate_base( |
| 1555 | self, |
| 1556 | mapper: Optional[Callable[..., Mapper[Any]]] = None, |
| 1557 | cls: Type[Any] = object, |
| 1558 | name: str = "Base", |
| 1559 | metaclass: Type[Any] = DeclarativeMeta, |
| 1560 | ) -> Any: |
| 1561 | """Generate a declarative base class. |
| 1562 | |
| 1563 | Classes that inherit from the returned class object will be |
| 1564 | automatically mapped using declarative mapping. |
| 1565 | |
| 1566 | E.g.:: |
| 1567 | |
| 1568 | from sqlalchemy.orm import registry |
| 1569 | |
| 1570 | mapper_registry = registry() |
| 1571 | |
| 1572 | Base = mapper_registry.generate_base() |
| 1573 | |
| 1574 | |
| 1575 | class MyClass(Base): |
| 1576 | __tablename__ = "my_table" |
| 1577 | id = Column(Integer, primary_key=True) |
| 1578 | |
| 1579 | The above dynamically generated class is equivalent to the |
| 1580 | non-dynamic example below:: |
| 1581 | |
| 1582 | from sqlalchemy.orm import registry |
| 1583 | from sqlalchemy.orm.decl_api import DeclarativeMeta |
| 1584 | |
| 1585 | mapper_registry = registry() |
| 1586 | |
| 1587 | |
| 1588 | class Base(metaclass=DeclarativeMeta): |
| 1589 | __abstract__ = True |
| 1590 | registry = mapper_registry |
| 1591 | metadata = mapper_registry.metadata |
| 1592 | |
| 1593 | __init__ = mapper_registry.constructor |
| 1594 | |
| 1595 | .. versionchanged:: 2.0 Note that the |
| 1596 | :meth:`_orm.registry.generate_base` method is superseded by the new |
| 1597 | :class:`_orm.DeclarativeBase` class, which generates a new "base" |
| 1598 | class using subclassing, rather than return value of a function. |
| 1599 | This allows an approach that is compatible with :pep:`484` typing |
| 1600 | tools. |
| 1601 | |
| 1602 | The :meth:`_orm.registry.generate_base` method provides the |
| 1603 | implementation for the :func:`_orm.declarative_base` function, which |
| 1604 | creates the :class:`_orm.registry` and base class all at once. |
| 1605 | |
| 1606 | See the section :ref:`orm_declarative_mapping` for background and |
| 1607 | examples. |
| 1608 | |
| 1609 | :param mapper: |
| 1610 | An optional callable, defaults to :class:`_orm.Mapper`. |
| 1611 | This function is used to generate new :class:`_orm.Mapper` objects. |
no outgoing calls