Skip to content

ORMTableBase

ORMTableBase is the foundational mixin for all ORM tables in orm-loader.

It provides: - primary key introspection - column inspection - ID allocation helpers

It contains no domain logic.

This page is generated from in-code documentation.


API

Mixin for SQLAlchemy ORM-mapped tables providing structural introspection and helper utilities.

This base class intentionally avoids any domain or schema semantics and is designed to sit at the lowest level of a model hierarchy.

It provides: - mapper access - primary key discovery and extraction - column inspection helpers - required-field detection for ingestion - monotonic ID allocation support

allocator(session) classmethod

Create an ID allocator initialised from the current table state.

The allocator is initialised using the current maximum primary key value and can be used to generate monotonically increasing identifiers in environments without database-managed sequences.

Used to support autoincrementing primary keys on dialects without native sequence support (e.g., SQLite).

Parameters:

Name Type Description Default
session Session

An active SQLAlchemy session.

required

Returns:

Type Description
IdAllocator

An ID allocator initialised to the current maximum ID.

column_defaults() classmethod

Return the default of each non-key column that has one that can be supplied.

A server default given as text and a scalar Python default are returned as the value itself. A server default given as a SQL expression is returned as that expression, for the database to evaluate. A callable or context-sensitive default cannot be supplied for a whole file, so its column is not listed. A primary key never takes a default, because a file has to name it.

Returns:

Type Description
dict[str, Any]

A mapping of column name to the value, or SQL expression, of its default.

mapper_for() classmethod

Return the SQLAlchemy mapper associated with this ORM class.

This is a thin wrapper around sqlalchemy.inspect that provides a single, explicit access point for mapper inspection.

Returns:

Type Description
Mapper

The mapper associated with the ORM-mapped class.

Raises:

Type Description
TypeError

If the class is not a mapped SQLAlchemy ORM class.

max_id(session) classmethod

Return the maximum value of the primary key column.

This method only supports tables with a single-column primary key.

Parameters:

Name Type Description Default
session Session

A SQLAlchemy session.

required

Returns:

Type Description
int

The maximum primary key value, or 0 if the table is empty.

Raises:

Type Description
ValueError

If the table has a composite primary key.

model_columns() classmethod

Return the mapped columns a file can supply.

Columns are keyed by their database name, which is the name that file headers and staging tables use, even when Column.key differs. Computed columns are generated by the database and are not included.

Returns:

Type Description
dict[str, ColumnElement]

A mapping of database column name to column object.

pk_columns() classmethod

Return the primary key columns for the mapped table.

The columns are returned in mapper-defined order.

Returns:

Type Description
list[ColumnElement]

A list of primary key column objects.

Raises:

Type Description
ValueError

If the table has no primary key defined.

pk_names() classmethod

Return the primary key column names.

Returns:

Type Description
list[str]

A list of primary key column names.

required_columns() classmethod

Return column names that must be present in inbound data.

A column is required if it is a primary key, or if it is non-nullable and has no default that can be supplied (see column_defaults).

This method is intended for ingestion-time validation and does not attempt to enforce schema semantics beyond insert viability.

Returns:

Type Description
set[str]

A set of required column names.

sql_expression_defaults() classmethod

Return the defaults that are SQL expressions, which the database evaluates for each row.

Returns:

Type Description
dict[str, ClauseElement]

A mapping of column name to its SQL expression default.