这是代码:
from sqlalchemy import Table, Column, Index, String, MetaData, func, create_engine
md = MetaData()
tbl = Table('t1', md, Column('col1', String()))
idx = Index(
'idx1',
func.lower(tbl.c.col1),
postgresql_ops={'col1': 'text_pattern_ops'},
)
e = create_engine("postgresql://postgres:postgres@localhost/test", echo=True)
with e.connect() as c:
md.create_all(c)
输出
Column | Type | Modifiers
--------+-------------------+-----------
col1 | character varying |
Indexes:
"idx1" btree (lower(col1::text)) # NO text_pattern_ops!!!
答案 0 :(得分:2)
根据docs,您需要在表达式上添加标签:
idx = Index(
'idx1',
func.lower(tbl.c.col1).label('lower_col1'),
postgresql_ops={'lower_col1': 'text_pattern_ops'},
)