Relations

Relation declarations between tables for cascade-cleanup and FK validation.

A Relation captures a foreign-key reference from one table’s column(s) to another table’s column(s). Relations are typically derived from Column.foreign_key declarations on user schemas via resolve_relations_from_schemas(), but can also be constructed explicitly to override or extend the schema-derived set.

class algomancy_data.relations.Relation(child_table, child_cols, parent_table, parent_cols, parent_requires_child=False, track_partial_loss=False)[source]

Bases: object

A foreign-key relation between a child table and a parent table.

Used by CascadeDropTransformer (cascade cleanup) and ForeignKeyValidator.from_schemas (FK violation reporting). Typically built from Column.foreign_key declarations via resolve_relations_from_schemas(), but can be constructed explicitly to override or extend the schema-derived set.

child_table: str

Logical name of the child table (matches Schema.file_name()).

child_cols: Tuple[str, ...]

Tuple of column names on the child table that form the FK.

parent_table: str

Logical name of the parent (referenced) table.

parent_cols: Tuple[str, ...]

Tuple of column names on the parent table forming the referenced key.

parent_requires_child: bool = False

If True, parents with zero referencing children are dropped.

track_partial_loss: bool = False

If True, enables partial-loss cascade when paired with a snapshot.

property key: Tuple[str, Tuple[str, ...]]

(child_table, child_cols).

Type:

Identity used when merging relations

algomancy_data.relations.resolve_relations_from_schemas(schemas)[source]

Build a list of Relation objects from FK declarations on schemas.

Walks the Column.foreign_key declarations on each schema and groups columns sharing the same parent table into composite FKs. Within a single schema, all columns with foreign_key pointing at the same parent table are collapsed into one composite relation; the referenced parent columns are taken from each column’s foreign_key tuple in declaration order.

Parameters:

schemas (Sequence[Type[Schema]]) – Iterable of Schema subclasses to walk.

Returns:

List of relations, deduplicated by (child_table, child_cols).

Return type:

List[Relation]

algomancy_data.relations.merge_relations(base, override)[source]

Merge two relation lists; override wins on matching (child_table, child_cols).

Parameters:
  • base (Sequence[Relation]) – Base relations (typically schema-derived).

  • override (Sequence[Relation]) – Override relations (typically user-supplied extras).

Returns:

Combined list. Entries from override replace entries from base with the same key; remaining entries from override are appended.

Return type:

List[Relation]