DEV Community

Applying a Naming Convention to Constraints via SQLAlchemy & Alembic

James on February 14, 2019

Top Tip! So we're creating a new database schema, using SQLAlchemy for the ORM and Alembic for the schema management, (if you use Pyth...
Collapse
 
sonnk profile image
Nguyen Kim Son

There are some length limits for constraints, foreign keys, etc in MySQL (I'm not sure if this is the case for other database types) so in some extreme case, we might need to choose the constraint name explicitly.

Collapse
 
arashfatahzade profile image
Arash Fatahzade • Edited

I have signup to just say THANK YOU <3.
Worked like charm on postgres.

EDIT: postgres naming conventions:

metadata = MetaData(                                                             
    naming_convention={                                                          
        "ix": "%(column_0_label)s_idx",                                                                                  
        "uq": "%(table_name)s_%(column_0_name)s_key",                            
        "ck": "%(table_name)s_%(constraint_name)s_check",                        
        "fk": "%(table_name)s_%(column_0_name)s_%(referred_table_name)s_fkey",   
        "pk": "%(table_name)s_pkey"                                              
    }                                                                            
)