6
votes

First off. Apologies if this has been answered but I can not find the answer any where.

I need to define a lowercase index on a Flask-SQLAlchemy object.

The problem I have is I need a models username and email fields to be stored lowercase so that I can check them with User.query.filter(func.lower(username) == func.lower(username)).first()

Up to now I have always dealt with this by just inserting these fields as lowercase but in this particular instance I need the username to preserve the case that it was defined with.

I think I am on the right tracks but am running in to an issue I have not seen before.

class User(UserMixin, db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    # Indexes for username and email are defined below User
    # They are functional lowercase indexes
    username = db.Column(db.String(32))
    email = db.Column(db.String(255))
    password_hash = db.Column(db.String(255))

    ...

db.Index('ix_users_username', func.lower(User.username), unique=True)
db.Index('ix_users_email', func.lower(User.email), unique=True)

I can't see a problem with this. I am guessing that the func.lower() requires the Index to be defined separate from what I can tell with my other searches.

Now the problem comes when I run the migration I get the following errors:

INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added table 'users'
.../lib/python3.4/site-packages/alembic/util/messaging.py:69: UserWarning: autogenerate skipping functional index ix_users_email; not supported by SQLAlchemy reflection
  warnings.warn(msg)
.../lib/python3.4/site-packages/alembic/util/messaging.py:69: UserWarning: autogenerate skipping functional index ix_users_username; not supported by SQLAlchemy reflection

I'm not 100% sure why this isn't supported by reflection. I was expecting in my migration that it would add them as it did before but with it wrapped in lower.

I am not adverse to writing the migration manually (as I'm presuming its possible, not 100% sure how) but can anyone point out why this doesn't work out of the box?

Thanks in advance Joe

Update

I solved this by adding the following line in to the migration.

op.create_index('ix_users_username', 'users', [sa.text('lower(username)')])
op.create_index('ix_users_email', 'users', [sa.text('lower(email)')])
2
You'll need to write the migration manually. I'm guessing it's not supported because it's difficult to parse the expression for the functional index. SQLAlchemy renders SQL from an in-memory representation; it does not do the reverse. - univerio
Thanks @univerio. I presumed this would be the case, was just a little confused as I presumed it would be simpler. - Joe Doherty
@univero. I have modified the migration and all is good. One thing though. I have taken out the definition of the indexes because Alembic complains about it constantly. I just use the default (..., index=True, unique=True) on the column definition. Is this what you would do? - Joe Doherty
No. Your models should reflect the exact state your database schema is in, such that you can do metadata.create_all() to create a fresh database. If you wanted to silence the warnings, you can use include_object to exclude your indices. - univerio
Perfect! That is the conclusion I came to also. This has done the trick and thanks for the link. If you want the points for answering feel free. I can send an edit to the answer with exactly how I added the migration. I think this may be useful as an answer. - Joe Doherty

2 Answers

3
votes

You'd need to add the UNIQUE INDEX manually using alembic's execute method. Add something like this to your migration script.

from alembic import op
# ...

def upgrade():
    # ...
    op.execute(
        """ CREATE UNIQUE INDEX users_normalized_username
            ON users
            (lower(username))
        """
    )

def downgrade():
    # ...
    op.execute("DROP INDEX users_normalized_username")

You could also add a ColumnProperty so you have access the normalized representation of the username.

from sqlalchemy import Column, String, func, orm

class User(Base):
    __tablename__ = 'users'

    username = Column(String(32))
    normalized_username = orm.column_property(func.lower(username))

    # ...

This won't create an extra column in your database but you'll be able to query and compare against it.

session.query(User).filter(User.normalized_username == func.lower(username)).first()
0
votes

I found a hack to do it, in your model file you don't add a new index, but you modify an existing index. then run db migrate, it should be able to generate the alchemic python code for you. Then you just need to adjust the alchemic python code to rename it to the new index name. And modify the model file again to add your new index and revert the modification you did before for that existing index.