Skip to content

orm

Application ORM configuration.

DTO_KEY module-attribute

DTO_KEY = settings.api.DTO_INFO_KEY

The key we use to reference dto.DTOField in the SQLAlchemy info dict.

convention module-attribute

convention = {
    "ix": "ix_%(column_0_label)s",
    "uq": "uq_%(table_name)s_%(column_0_name)s",
    "ck": "ck_%(table_name)s_%(constraint_name)s",
    "fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
    "pk": "pk_%(table_name)s",
}

Templates for automated constraint name generation.

Base

Bases: DeclarativeBase

Base for all SQLAlchemy declarative models.

created class-attribute

created: Mapped[datetime] = mapped_column(
    default=datetime.now, info={DTO_KEY: dto.DTOField(mark=dto.Mark.READ_ONLY)}
)

Date/time of instance creation.

id class-attribute

id: Mapped[UUID] = mapped_column(
    default=uuid4, primary_key=True, info={DTO_KEY: dto.DTOField(mark=dto.Mark.READ_ONLY)}
)

Primary key column.

updated class-attribute

updated: Mapped[datetime] = mapped_column(
    default=datetime.now, info={DTO_KEY: dto.DTOField(mark=dto.Mark.READ_ONLY)}
)

Date/time of instance update.

__tablename__

__tablename__()

Infer table name from class name.

touch_updated_timestamp

touch_updated_timestamp(session, *_)

Set timestamp on update.

Called from SQLAlchemy's before_flush event to bump the updated timestamp on modified instances.

Parameters:

Name Type Description Default
session Session

The sync Session instance that underlies the async session.

required