How to customize base declarative class to add naming conventions

116 views
Skip to first unread message

rdeb...@gmail.com

unread,
Sep 4, 2018, 10:47:46 AM9/4/18
to sqlalchemy
I'd like to create a mixin to specify naming conventions.

I tried both:

class Base:
metadata = MetaData(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)",
})

and

class Base:
@declared_attr
def metadata(cls):
return MetaData(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)",
})

But if I inspect a model created using this base I always got:

>>> Test.metadata.naming_convention
immutabledict({'ix': 'ix_%(column_0_label)s'})

while I correctly have:

>>> Base.metadata.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)'}


What is the correct way to do it? what am i doing wrong? Should I do this in my migration tool (alembic) ?
Also would it works for unique constraint on multiple column or do we have to name them explicitly.

Thanks a lot!

Simon King

unread,
Sep 4, 2018, 11:11:01 AM9/4/18
to sqlal...@googlegroups.com
Is it important to you to do this via a mixin? declarative_base
accepts a "metadata" parameter, so something like this should work:

metadata = MetaData(naming_convention={...})
Base = declarative_base(metadata=metadata)

Hope that helps,

Simon

René-paul Debroize

unread,
Sep 5, 2018, 5:18:44 AM9/5/18
to sqlal...@googlegroups.com
It would have been great to be able to do it via a mixin, I have several DB using decalarative base constructed with this Base mixin and I liked to have the same naming_convention for all the DBs without repeating myself.
If it's not I guess i can still manage to find an acceptable way of doing it using the decalarative_base arg.

Thanks.

--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example.  See  http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Derek Lambert

unread,
Sep 5, 2018, 11:17:23 AM9/5/18
to sqlalchemy
Just define the naming convention dict in a separate file and import it into each declarative base?
Reply all
Reply to author
Forward
0 new messages