我正在尝试使用以下代码将一些名称与我在 Postgresql 数据库中已有的名称相匹配:
I'm trying to match some names with the names I already have in my Postgresql database, using the following code:
last_like = '{last_name}%'.format(last_name) matches = Session.query(MyTable).filter(or_( MyTable.name.ilike(last_like), MyTable.other_names.any(last_like, operator=ilike_op), )).all()本质上,它尝试匹配 name 列 或 以数组形式存储在 other_names 列中的任何其他名称.
Essentially it tries to match the name column or any of the other names stored as an array in the other_names column.
但我明白了:
KeyError: <function ilike_op at 0x7fb5269e2500>我做错了什么?
推荐答案要使用 postgresql 数组字段,您需要使用 unnest() 函数.但是你不能在 where 子句中使用 unnest() 的结果.
For use postgresql array field you need to use unnest() function. But you can't use result of unnest() in where clause.
相反,您可以使用 array_to_string 函数.搜索 other_names 的字符串将产生相同的效果
Instead, you can use array_to_string function. Searching on string of other_names will give the same effect
from sqlalchemy import func as F last_like = "%qq%" matches = session.query(MyTable).filter(or_( MyTable.name.ilike(last_like), F.array_to_string(MyTable.other_names, ',').ilike(last_like), )).all()更多推荐
如何在 postgresql 数组字段上使用 ilike sqlalchemy?
发布评论