MCPcopy Create free account
hub / github.com/sqlalchemy/sqlalchemy / generate_base

Method generate_base

lib/sqlalchemy/orm/decl_api.py:1508–1611  ·  view source on GitHub ↗

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,
    )

Source from the content-addressed store, hash-verified

1506 instrumentation._instrumentation_factory.unregister(class_)
1507
1508 def generate_base(
1509 self,
1510 mapper: Optional[Callable[..., Mapper[Any]]] = None,
1511 cls: Type[Any] = object,
1512 name: str = "Base",
1513 metaclass: Type[Any] = DeclarativeMeta,
1514 ) -> Any:
1515 """Generate a declarative base class.
1516
1517 Classes that inherit from the returned class object will be
1518 automatically mapped using declarative mapping.
1519
1520 E.g.::
1521
1522 from sqlalchemy.orm import registry
1523
1524 mapper_registry = registry()
1525
1526 Base = mapper_registry.generate_base()
1527
1528
1529 class MyClass(Base):
1530 __tablename__ = "my_table"
1531 id = Column(Integer, primary_key=True)
1532
1533 The above dynamically generated class is equivalent to the
1534 non-dynamic example below::
1535
1536 from sqlalchemy.orm import registry
1537 from sqlalchemy.orm.decl_api import DeclarativeMeta
1538
1539 mapper_registry = registry()
1540
1541
1542 class Base(metaclass=DeclarativeMeta):
1543 __abstract__ = True
1544 registry = mapper_registry
1545 metadata = mapper_registry.metadata
1546
1547 __init__ = mapper_registry.constructor
1548
1549 .. versionchanged:: 2.0 Note that the
1550 :meth:`_orm.registry.generate_base` method is superseded by the new
1551 :class:`_orm.DeclarativeBase` class, which generates a new "base"
1552 class using subclassing, rather than return value of a function.
1553 This allows an approach that is compatible with :pep:`484` typing
1554 tools.
1555
1556 The :meth:`_orm.registry.generate_base` method provides the
1557 implementation for the :func:`_orm.declarative_base` function, which
1558 creates the :class:`_orm.registry` and base class all at once.
1559
1560 See the section :ref:`orm_declarative_mapping` for background and
1561 examples.
1562
1563 :param mapper:
1564 An optional callable, defaults to :class:`_orm.Mapper`.
1565 This function is used to generate new :class:`_orm.Mapper` objects.

Callers 15

decorateMethod · 0.95
declarative_baseFunction · 0.80
setup_testMethod · 0.80
_fixture_oneMethod · 0.80
_fixture_twoMethod · 0.80
_fixture_threeMethod · 0.80
_fixture_fourMethod · 0.80
test_fixture_fiveMethod · 0.80
test_clauseelement_pjMethod · 0.80

Calls

no outgoing calls

Tested by 15

setup_testMethod · 0.64
_fixture_oneMethod · 0.64
_fixture_twoMethod · 0.64
_fixture_threeMethod · 0.64
_fixture_fourMethod · 0.64
test_fixture_fiveMethod · 0.64
test_clauseelement_pjMethod · 0.64