2014-02-27 72 views
1

我試圖創建一個函數索引(使用PostgreSQL函數lower())。我得到這個錯誤使用蒸餾器創建表時:Flask-SQLAlchemy:創建函數索引

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)) 
+1

這些ORM經常只理解基本的SQL功能。如果您需要手動將索引字詞編寫爲SQL文本,請不要感到驚訝。 –

+1

根據http://docs.sqlalchemy.org/en/rel_0_8/core/constraints.html#functional-indexes它應該工作,但我得到了上述錯誤。 – user3362454

回答

0

不幸的是蒸餾器無法生成函數索引正確的版本,但(我希望)。您遷移應該看起來像:

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

你可以這樣做:

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)) 

此外,謝謝你這個問題,我知道如何創建索引!