Naming conventions¶
Use codegen_database.construct_naming_conventions_dict() as the naming
convention for application metadata:
from sqlalchemy import MetaData
from codegen_database import construct_naming_conventions_dict
metadata = MetaData(
naming_convention=construct_naming_conventions_dict(),
)
The convention automatically names indexes, unique constraints, checks, foreign keys, and primary keys. Names retain the object type, table, and columns where available, and end in a deterministic hash. The hash distinguishes otherwise similar objects such as foreign keys with different targets or options, check expressions, and expression indexes.
Names are limited to PostgreSQL’s 63-byte identifier limit by default.
Pass max_length to use a smaller limit; the hash suffix is retained after
truncation. Exact duplicate declarations have the same name.
Changing a naming convention renames generated database objects. Existing databases require an Alembic migration that renames their constraints and indexes before adopting this convention.
Applications can override any convention key before creating MetaData:
convention = construct_naming_conventions_dict()
convention["ix"] = "ix_%(table_name)s_%(column_0_name)s"
metadata = MetaData(naming_convention=convention)