Naming convention for primary key and Flask, Sqlalchemy

319 views
Skip to first unread message

Gastón Avila

unread,
Jan 25, 2016, 2:51:48 PM1/25/16
to sqlalchemy
Hi all,

I have set up specific naming conventions in a Flask-Sqlalchemy app using the metadata argument and it all seems to work except for naming PRIMARY KEY constraints. Here is what I have

self.db.init_app(app)
self.db.Model.metadata = MetaData(
 schema
=self.app.config['SQLALCHEMY_DATABASE_SCHEMA'],
 naming_convention
={
 
"columns": columns,
 
"ix": '%(table_name)s_%(columns)s_idx',
 
"uq": "%(table_name)s_%(columns)s_key",
 
"fk": "%(table_name)s_%(columns)s_fkey",
 
"pk": "%(table_name)s_%(columns)s_pkey"
 
}
)

and the columns helper is defined by:
def columns(constraint, table):
   
return "_".join([col.name for col in constraint.columns])

It works for every single kind except for the primary key property of the naming convention obj. When printed out, the helper receives a constraint argument that when
print constraint
is added, gives PrimaryKeyConstraint() so no columns can be gotten from it.

Does anyone know what could be causing this? I have a guess that it might be this flask-sqlalchemy pluging but cannot figure it out.

Thanks a lot!


Mike Bayer

unread,
Jan 25, 2016, 4:13:23 PM1/25/16
to sqlal...@googlegroups.com


On 01/25/2016 02:51 PM, Gastón Avila wrote:
> Hi all,
>
> I have set up specific naming conventions in a Flask-Sqlalchemy app
> using the metadata argument and it all seems to work except for naming
> PRIMARY KEY constraints. Here is what I have
>
> |
> self.db.init_app(app)
> self.db.Model.metadata =MetaData(
> schema=self.app.config['SQLALCHEMY_DATABASE_SCHEMA'],
> naming_convention={
> "columns":columns,
> "ix":'%(table_name)s_%(columns)s_idx',
> "uq":"%(table_name)s_%(columns)s_key",
> "fk":"%(table_name)s_%(columns)s_fkey",
> "pk":"%(table_name)s_%(columns)s_pkey"
> }
> )
> |
>
> and the *columns* helper is defined by:
> |
> defcolumns(constraint,table):
> return"_".join([col.name forcol inconstraint.columns])
> |
>
> It works for every single kind except for the *primary key* property of
> the naming convention obj. When printed out, the helper receives a
> constraint argument that when
> |
> printconstraint
> |
> is added, gives *PrimaryKeyConstraint()* so no columns can be gotten
> from it.

Table makes its own PrimaryKeyConstraint up front and the naming
convention is not "deferred", that is the convention is called
immediately, so you'd need to specify a PrimaryKeyConstraint explicitly;
the good news is you can forego the "primary_key=True" flag within
columns when working this way:

def columns(constraint, table):
return "_".join([col.name for col in constraint.columns])

from sqlalchemy import MetaData, Integer, Column, Table, create_engine,
PrimaryKeyConstraint

m = MetaData(
naming_convention={
"columns": columns,
"ix": '%(table_name)s_%(columns)s_idx',
"uq": "%(table_name)s_%(columns)s_key",
"fk": "%(table_name)s_%(columns)s_fkey",
"pk": "%(table_name)s_%(columns)s_pkey"
}
)

t = Table('t', m, Column('id', Integer), PrimaryKeyConstraint('id'))

e = create_engine("sqlite://", echo=True)
m.create_all(e)



>
> Does anyone know what could be causing this? I have a guess that it
> might be this flask-sqlalchemy pluging but cannot figure it out.
>
> Thanks a lot!
>
>
> --
> 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
> <mailto:sqlalchemy+...@googlegroups.com>.
> To post to this group, send email to sqlal...@googlegroups.com
> <mailto:sqlal...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

Gastón J. Avila

unread,
Jan 26, 2016, 9:22:30 AM1/26/16
to sqlal...@googlegroups.com
I had to use 
   __tablename__ = 'some_name'
   __table_args__ 
= (PrimaryKeyConstraint('id'), )
in my declarative models to get the same result.

Thanks Mike, that clears it all up.

You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/GjNmo882Di8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages