3

I'm trying to create a functional index (with the PostgreSQL function lower()). I get this error when using Alembic to create the table:

sqlalchemy.exc.ProgrammingError: (ProgrammingError) column "lower" does not exist
 'CREATE INDEX ix_cities_name ON cities (lower)' {}

Here's the code:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy

app = Flask(__name__)
db = SQLAlchemy(app)

class City(db.Model):
    __tablename__ = 'cities'

    name = db.Column(db.String(100), nullable=False)

db.Index('ix_cities_name', db.func.lower(db.metadata.tables['cities'].c.name))
1

2 Answers 2

3

You can do like this:

from sqlalchemy import func

db.Index('ix_cities_name', func.lower(City.name))

reference to:

Functional indexes are supported as well, typically by using the
:data:`.func` construct in conjunction with table-bound
:class:`.Column` objects::

    Index("some_index", func.lower(sometable.c.name))

In addition, thank you for this question, I know how to create the index!

Sign up to request clarification or add additional context in comments.

Comments

1

Unfortunately alembic can't generate correct revision for functional indexes yet (I hope). You migration should be looks like:

op.execute("CREATE INDEX ix_cities_name ON cities (LOWER(name))")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.