Naming conventions¶
codegen_database generates database objects automatically — tables, views, functions,
and triggers — and uses a naming convention system to derive their names
deterministically. Conventions are stored in the SQLAlchemy
MetaData naming_convention dict, giving you
one place to control every generated name in your schema.
This builds directly on SQLAlchemy’s built-in constraint naming conventions feature. If you are not already familiar with it, read that guide first — codegen_database extends the same mechanism rather than inventing a parallel system.
Setting up naming conventions¶
Pass construct_naming_conventions_dict() to your
MetaData when you create it:
from sqlalchemy import MetaData
from codegen_database import construct_naming_conventions_dict
metadata = MetaData(naming_convention=construct_naming_conventions_dict())
This populates the standard SQLAlchemy constraint keys (pk, fk,
uq, ix, ck) with length-safe token callables, and also seeds the
codegen_database-specific keys that plugins use for views, functions, and triggers.
Without it, SQLAlchemy leaves constraint names as None and Alembic
autogenerate cannot track them reliably.
The max_length parameter (default 63) controls the maximum character
length of generated constraint names. PostgreSQL’s identifier limit is 63
bytes, so the default matches that. Names that would exceed this limit are
truncated and an 8-character MD5 digest is appended to avoid collisions:
metadata = MetaData(naming_convention=construct_naming_conventions_dict(max_length=63))
Overriding a specific convention¶
Every naming convention key is just a string in the naming_convention dict.
Set it after calling construct_naming_conventions_dict() to override
only that key while keeping all others:
metadata = MetaData(naming_convention=construct_naming_conventions_dict())
# Give the "prices" append-only dimension custom table names.
metadata.naming_convention["append_only_root"] = "%(table_name)s_ids"
metadata.naming_convention["append_only_attributes"] = "%(table_name)s_log"
Overrides are global — they apply to every factory that uses that key. To target a specific dimension only, embed the table name directly in the template:
# Only renames the root table for "prices"; other dimensions are unaffected.
metadata.naming_convention["append_only_root"] = "price_ids"
Templates use Python %-style interpolation. The available substitution
variables for each key are listed in the reference below.
Convention reference¶
Constraint names¶
These keys are provided by construct_naming_conventions_dict() and
govern how SQLAlchemy names database constraints and indexes. They follow the
same %(token)s interpolation format described in the SQLAlchemy
constraint naming conventions guide.
Key |
Default template |
Example output |
|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
The name tokens (pk_name, fk_name, etc.) are callable objects that
build the full name from the table and column names. SQLAlchemy’s naming
convention system supports both string templates and callables as values — the
callables here are an example of that. You should not override them directly;
override the corresponding template key (pk, fk, etc.) instead if you
need a different format.
Simple dimension¶
Used by SimpleTablePlugin
for the raw backing table, and by
InsteadOfTriggerPlugin when
creating INSTEAD OF triggers on the factory dimension view.
Available substitutions: %(table_name)s, %(schema)s, %(op)s
(insert, update, or delete).
Key |
Default template |
Names |
|---|---|---|
|
|
Raw backing table |
|
|
Factory view INSTEAD OF trigger function |
|
|
Factory view INSTEAD OF trigger |
Append-only dimension¶
Used by AppendOnlyTablePlugin for the
two backing tables, and by
InsteadOfTriggerPlugin for INSTEAD OF
triggers on the dimension view.
Available substitutions: %(table_name)s, %(schema)s, %(op)s.
Key |
Default template |
Names |
|---|---|---|
|
|
Entity (root) table |
|
|
Attributes log table |
|
|
INSTEAD OF trigger function |
|
|
INSTEAD OF trigger |
EAV dimension¶
Used by EAVTablePlugin for the two backing
tables, and by InsteadOfTriggerPlugin for INSTEAD OF
triggers on the dimension view.
Available substitutions: %(table_name)s, %(schema)s, %(op)s.
Key |
Default template |
Names |
|---|---|---|
|
|
Entity (root) table |
|
|
Attribute log table |
|
|
INSTEAD OF trigger function |
|
|
INSTEAD OF trigger |
Check plugin¶
Used by TriggerCheckPlugin when adding
validation triggers to EAV dimensions.
Available substitutions: %(table_name)s, %(schema)s, %(op)s.
Key |
Default template |
Names |
|---|---|---|
|
|
Validation trigger function |
|
|
Validation trigger |
Ledger¶
Used by the ledger factory plugins. See Ledger tables for full documentation of each plugin.
Available substitutions: %(table_name)s, %(schema)s, %(op)s.
Key |
Default template |
Names |
|---|---|---|
|
|
Raw backing table |
|
|
Factory view INSTEAD OF trigger function |
|
|
Factory view INSTEAD OF trigger |
|
|
Balance summary view |
|
|
Latest-row view ( |
|
|
Balance enforcement trigger function |
|
|
Balance enforcement trigger |
|
|
Double-entry validation trigger function |
|
|
Double-entry validation trigger |