Skip to content

self_referencing

sqllocks_spindle.engine.strategies.self_referencing

Self-referencing strategy — generate a parent FK within the same table.

Builds a proper multi-level hierarchy where rows are assigned to levels and each non-root row gets a parent from the level above.

The PK column must already be generated (sequence/uuid) before this strategy runs — which is guaranteed by the column ordering in TableGenerator (PKs first).

Level assignments and parent IDs are stashed in ctx.current_table under _sr_{table_name}_level so that a subsequent level column can use record_field-style retrieval via the self_ref_field strategy (or just reference it directly).

Example schema

"product_category": { "primary_key": ["category_id"], "columns": { "category_id": {"generator": {"strategy": "sequence", "start": 1}}, "parent_category_id": { "generator": { "strategy": "self_referencing", "pk_column": "category_id", "levels": 3, "root_count": 8 } }, "level": { "generator": { "strategy": "self_ref_field", "field": "level" } } } }

Classes

SelfReferencingStrategy

Bases: Strategy

Assign parent IDs within the same table to form a level hierarchy.

Row allocation
  • The first root_count rows become level-1 roots (parent = NULL)
  • Remaining rows are split evenly across levels 2..N
  • Each non-root row is assigned a random parent from the level above

Stashes level assignments into ctx.current_table[sr_level] for use by a downstream 'level' column.

SelfRefFieldStrategy

Bases: Strategy

Read a field stashed by SelfReferencingStrategy.

Example: reads level assignments stored by the self_referencing column.

"level": {
    "generator": {"strategy": "self_ref_field", "field": "level"}
}