can't use __table_args__ add indexes

582 views
Skip to first unread message

Yingchen Zhang

unread,
Jul 23, 2018, 1:57:47 PM7/23/18
to sqlalchemy
class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.BigInteger, primary_key=True)
    name = db.Column(db.VARCHAR(50), nullable=False, unique=True)
    email = db.Column(db.VARCHAR(200), nullable=False)

    mobile = db.Column(db.VARCHAR(20))

    created_at = db.Column(db.TIMESTAMP(False), nullable=False, default=func.now())

    __table_args__ = (
        Index('idx_users_name', func.lower('name'), unique=True),
        Index('idx_users_email', func.lower('email'), unique=True)
    )

    def __init__(self):
        if not self.created_at:
            self.created_at = now()

    def __repr__(self):
        return '<User %s>' % self.name

got a warning:

UserWarning: autogenerate skipping functional index idx_users_email; not supported by SQLAlchemy reflection  warnings.warn(msg)
UserWarning: autogenerate skipping functional index idx_users_name; not supported by SQLAlchemy reflection  warnings.warn(msg)


Mike Bayer

unread,
Jul 23, 2018, 2:06:06 PM7/23/18
to sqlal...@googlegroups.com
your code is correct. it sounds like you are attempting to use
autogenerate in Alembic to add these indexes in a new migration. you
have to specify them manually because autogenerate does not support
function-based indexes (e.g. the lower() function).


>
>
> --
> 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.
Message has been deleted

Yingchen Zhang

unread,
Jul 23, 2018, 4:02:24 PM7/23/18
to sqlalchemy
ok, tks.

so I must re-edit the migrations file?

在 2018年7月24日星期二 UTC+8上午2:06:06,Mike Bayer写道:

Mike Bayer

unread,
Jul 23, 2018, 6:21:18 PM7/23/18
to sqlal...@googlegroups.com
yes
Reply all
Reply to author
Forward
0 new messages