繁体   English   中英

Psycopg2“运算符不存在:整数[] =整数”错误

[英]Psycopg2 “operator does not exist: integer[] = integer” error

给定一个称为表namespace_listINTEGER[]柱称为namespace_ids

我有以下查询:

query = SELECT * FROM namespace_list WHERE namespace_ids = ANY(%s) 
and application_id=%s

我执行的是:

cur.execute(query, data)

以及数据的位置:

data = ([1, 2], 1)

我收到以下错误:

operator does not exist: integer[] = integer at character 50\nHINT:
No operator matches the given name and argument type(s).
You might need to add explicit type casts.

为什么这不起作用? 看看http://www.postgresql.org/message-id/CAG7mmoxZdRdatjWSYLBCVGJ4CPMa38ARSZByWe9xrWHKP0mB1g@mail.gmail.com ,以及Postgres Arrays上的其他教程,似乎我有正确的查询。

我也关注http://initd.org/psycopg/docs/usage.html#adapt-list的例子。 我的查询有什么问题,或者我使用psycopg2的数组的方式?

问题是SQL表达式:

<column> = ANY(<array>)

如果<column>量值等于<array>逐个比较值的任何值,则返回true。 但是你的列不是标量值,它是一个数组,这就是为什么PostgreSQL说:

operator does not exist: integer[] = integer

将数组(左)与任何整数(右)进行比较的运算符不存在。 要解决此问题,您可以使用交集运算符( && )(如果需要仅匹配来自两个集合的一个id)或等值运算符( = ),如果要匹配所有数组元素:

SELECT * FROM namespace_list WHERE namespace_ids && %s and application_id=%s

这里的技巧是psycopg将Python列表转换为文字数组( ARRAY[...] ),您可以在标准SQL中使用它们的任何地方使用它们。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM