From 3eb2456fd614ced3b15518f271567aa8b718510c Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Fri, 30 Apr 2021 16:59:29 -0700 Subject: [PATCH] Add naming convention to metadata --- amanuensis/database.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/amanuensis/database.py b/amanuensis/database.py index af00d45..b49bbaa 100644 --- a/amanuensis/database.py +++ b/amanuensis/database.py @@ -1,4 +1,4 @@ -from sqlalchemy import create_engine +from sqlalchemy import create_engine, MetaData from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import scoped_session from sqlalchemy.orm import sessionmaker @@ -6,8 +6,17 @@ from sqlalchemy.orm import sessionmaker 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 session = scoped_session(sessionmaker(bind=engine)) # Base class for ORM models -ModelBase = declarative_base(engine) +ModelBase = declarative_base(metadata=metadata)