我试图创建一个功能索引(PostgreSQL函数lower())。使用Alembic创建表时出现此错误:
sqlalchemy.exc.ProgrammingError: (ProgrammingError) column "lower" does not exist
'CREATE INDEX ix_cities_name ON cities (lower)' {}
以下是代码:
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))
答案 0 :(得分:2)
你可以这样做:
from sqlalchemy import func
db.Index('ix_cities_name', func.lower(City.name))
参考:
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))
另外,感谢您提出这个问题,我知道如何创建索引!
答案 1 :(得分:0)
不幸的是,alembic无法为功能索引生成正确的修订版(我希望)。您的迁移应该如下所示:
op.execute("CREATE INDEX ix_cities_name ON cities (LOWER(name))")