Add naming convention to metadata

This commit is contained in:
Tim Van Baak 2021-04-30 16:59:29 -07:00
parent 2085aae0d4
commit 3eb2456fd6
1 changed files with 11 additions and 2 deletions

View File

@ -1,4 +1,4 @@
from sqlalchemy import create_engine from sqlalchemy import create_engine, MetaData
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session from sqlalchemy.orm import scoped_session
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker
@ -6,8 +6,17 @@ from sqlalchemy.orm import sessionmaker
engine = create_engine('sqlite:///:memory:') engine = create_engine('sqlite:///:memory:')
# Define naming conventions for generated constraints
metadata = MetaData(bind=engine, naming_convention={
"ix": "ix_%(column_0_label)s",
"uq": "uq_%(table_name)s_%(column_0_name)s",
"ck": "ck_%(table_name)s_%(constraint_name)s",
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
"pk": "pk_%(table_name)s"
})
# Thread-safe db session # Thread-safe db session
session = scoped_session(sessionmaker(bind=engine)) session = scoped_session(sessionmaker(bind=engine))
# Base class for ORM models # Base class for ORM models
ModelBase = declarative_base(engine) ModelBase = declarative_base(metadata=metadata)