【发布时间】:2015-03-31 19:17:41
【问题描述】:
背景
我在 SQLAlchemy 对象上定义了一个复合索引,比如:
class Shirt(Base):
__tablename__ = 'shirt'
id = Column(Integer, primary_key=True)
size = Column(String(32)) # e.g. small, medium large
color = Column(String(32)) # e.g. blue, red, white
Index('color_size', Shirt.size, Shirt.color)
问题
我现在想利用color_size 综合索引搜索small 和red 衬衫。
如何编写此查询?
使用and_() 会自动利用索引吗?
例如:
results = Shirt.query.filter( and_(Shirt.size=='small', Shirt.color=='red') ).all()
【问题讨论】:
标签: python sqlalchemy flask-sqlalchemy composite-key