Skip to content

base

SQLAlchemy DeclarativeBase, MetaData, and registry objects.

Import this Base into SQLAlchemy model files and let classes inherit from the DeclarativeBase declared here.

The registry() function sets the global SQLAlchemy registry for the DeclarativeBase object.

Note

Docs for DeclarativeBase and registry()

Docs for MetaData object

Base

Bases: DeclarativeBase

Default/Base class for SQLAlchemy models.

Description:

Child classes inheriting from this Base object will be treated as SQLAlchemy models. Set child class tables with __tablename__ = ....

Global defaults can be set on this object (i.e. a SQLAlchemy registry), and will be inherited/accessible by all child classes.

Note

When this class is instantiated, it will not be of type sqlalchemy.orm.DeclarativeBase; Because of the way this class is intialized, its type will be sqlalchemy.orm.decl_api.DeclarativeAttributeIntercept

Parameters:

Name Type Description Default
registry Registry

A registry object for the Base class

required
metadata MetaData

A MetaData object, with data about the Base class

required
Source code in src/red_utils/ext/sqlalchemy_utils/base.py
class Base(so.DeclarativeBase):
    """Default/Base class for SQLAlchemy models.

    Description:

    Child classes inheriting from this Base object will be treated as SQLAlchemy
    models. Set child class tables with `__tablename__ = ....`

    Global defaults can be set on this object (i.e. a SQLAlchemy registry), and will
    be inherited/accessible by all child classes.

    !!! note

        When this class is instantiated, it will not be of type sqlalchemy.orm.DeclarativeBase;
            Because of the way this class is intialized, its type will be
            sqlalchemy.orm.decl_api.DeclarativeAttributeIntercept

    Params:
        registry (sqlalchemy.Registry): A `registry` object for the `Base` class
        metadata (sqlalchemy.MetaData): A `MetaData` object, with data about the `Base` class
    """

    registry: so.registry = reg
    metadata: sa.MetaData = metadata