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.