Just define the naming convention dict in a separate file and import it into each declarative base?
On Wednesday, September 5, 2018 at 4:18:44 AM UTC-5, René-paul Debroize wrote: > > 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. > > Le mar. 4 sept. 2018 à 17:10, Simon King <si...@simonking.org.uk > <javascript:>> a écrit : > >> On Tue, Sep 4, 2018 at 3:48 PM <rdeb...@gmail.com <javascript:>> wrote: >> > >> > 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. >> > >> >> 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 >> >> -- >> 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 <javascript:>. >> To post to this group, send email to sqlal...@googlegroups.com >> <javascript:>. >> Visit this group at https://groups.google.com/group/sqlalchemy. >> For more options, visit https://groups.google.com/d/optout. >> > -- 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+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.