[英]Sqlalchemy query to array column using LIKE
I have table File with column file_names which is ARRAY type.我有列 file_names 的表文件,它是 ARRAY 类型。 W would like to build query searching records where any of array element is like "abc*" using wildcard. W 想使用通配符构建查询搜索记录,其中任何数组元素都类似于“abc*”。
I tried:我试过:
File.names.any("abc%", operator= operators.like_op).all()
but it doesn't work.但它不起作用。 It return sql:它返回sql:
SELECT file.id, file.names FROM file WHERE 'abc%' LIKE ANY(file.names)
but it return empty result.但它返回空结果。 Do you have any suggestions, how to build such query?您有什么建议,如何构建这样的查询?
the like
operator applies to a text, not to an array, so you need to find a way to convert your array into text before applying the like
operator. like
运算符适用于文本,而不是数组,因此在应用like
运算符之前,您需要找到一种将数组转换为文本的方法。 To do so in sql you can use array_to_string()
function, see the manual .要在 sql 中执行此操作,您可以使用array_to_string()
函数,请参阅手册。
I found solution in SQL:我在 SQL 中找到了解决方案:
SELECT file.id, file.names FROM file WHERE EXISTS (SELECT 1 FROM unnest(file.names) AS n
WHERE n LIKE 'abc%')
and it works.它有效。 But I don't know how to ask such query in sqlalchemy.但我不知道如何在 sqlalchemy 中提出这样的查询。 Any ideas?有任何想法吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.