Skip to content

Base classes

TIP

This is reference documentation. To see how to use these classes to build models, head to the tutorials or how-to guides.

SQLModelBase

python
from sqlmodel_ext import SQLModelBase

Root class for all sqlmodel-ext models. Inherits from SQLModel and uses the custom metaclass __DeclarativeMeta.

model_config:

KeyValueDescription
use_attribute_docstringsTrueThe """...""" below a field is automatically used as its description
validate_by_nameTrueAllow validation by field name (even when an alias is set)
extra'forbid'Passing an undeclared field raises ValidationError

Class methods:

python
@classmethod
def get_computed_field_names(cls) -> set[str]

Returns the set of all @computed_field field names.

Inheritance patterns:

  • class XxxBase(SQLModelBase) — pure data model (no table), used for API input/output
  • class Xxx(XxxBase, TableBaseMixin, table=True) — table-backed model

ExtraIgnoreModelBase

python
from sqlmodel_ext import ExtraIgnoreModelBase

Inherits from SQLModelBase but with extra='ignore': unknown fields are silently dropped while a WARNING is logged.

model_config:

KeyValueDescription
use_attribute_docstringsTrueSame as SQLModelBase
validate_by_nameTrueSame as SQLModelBase
extra'ignore'Unknown fields are dropped (no error)

Validator:

python
@model_validator(mode='before')
@classmethod
def _warn_unknown_fields(cls, data: Any) -> Any

If the input is a dict containing fields not declared on the model, logs a WARNING. alias and validation_alias count as known field names.

Use cases: third-party API responses, external WebSocket messages, JSON inputs whose schema may evolve.

TableBaseMixin

python
from sqlmodel_ext import TableBaseMixin

Adds an auto-incrementing integer primary key and CRUD methods to a model.

Inherits: AsyncAttrs (provides await obj.awaitable_attrs.xxx syntax).

Class marker:

python
_has_table_mixin: ClassVar[bool] = True

Lets the metaclass identify "this is a table class" and automatically apply table=True.

Fields:

FieldTypeDatabase behavior
idint | NonePrimary key, auto-generated (SERIAL / INTEGER PRIMARY KEY)
created_atdatetimeSet on insert via default_factory=now
updated_atdatetimeRefreshed on every UPDATE via onupdate=now

Methods: full CRUD signatures live in CRUD methods.

UUIDTableBaseMixin

python
from sqlmodel_ext import UUIDTableBaseMixin

UUID-keyed variant of TableBaseMixin.

Fields:

FieldTypeDatabase behavior
iduuid.UUIDPrimary key, default_factory=uuid.uuid4
created_atdatetimeSame as TableBaseMixin
updated_atdatetimeSame as TableBaseMixin

Type-precise overrides:

UUIDTableBaseMixin overloads get_one() / get_exist_one() so that the id parameter is typed as uuid.UUID rather than int.

RecordNotFoundError

python
from sqlmodel_ext import RecordNotFoundError

Raised by get_exist_one() when no record is found and FastAPI is not installed. When FastAPI is installed, HTTPException(404) is raised instead.

Detection logic: at module import time, import fastapi is attempted; the result is cached in _HAS_FASTAPI.