ProgrammingError:运算符不存在:bigint [] = integer []

时间:2019-01-22 08:44:38

标签: arrays python-3.x postgresql psycopg2

我正在使用Python 3和Postgres软件包(https://pypi.org/project/postgres/)。

我有一张表,其中的一列是BIGINT的数组。当我尝试使用python数字列表处理此列(选择,插入等)时,出现错误。

出现此错误的原因似乎是psycopg2将列表调整为一个INTEGER数组而不是BIGINT数组。它建议进行显式转换,但我在psycopg2文档中找不到如何执行此操作。 (当我传递的数字列表太大而无法容纳4个字节,即INTEGER的大小时,错误消失了)。

这是我运行的代码:

db.all("SELECT phase from messages where recipients=%(reps)s", {'reps':[12,34]})

还发生在:

db.all("SELECT phase from messages where recipients=ARRAY[12, 34]")

这是我收到的错误消息:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/yoni/.local/lib/python3.6/site-packages/postgres/__init__.py", line 552, in all
    return cursor.all(sql, parameters)
  File "/home/yoni/.local/lib/python3.6/site-packages/postgres/cursors.py", line 145, in all
    self.execute(sql, parameters)
  File "/home/yoni/.local/lib/python3.6/site-packages/psycopg2/extras.py", line 313, in execute
    return super(NamedTupleCursor, self).execute(query, vars)
psycopg2.ProgrammingError: operator does not exist: bigint[] = integer[]
LINE 1: SELECT phase from messages where recepients=ARRAY[12,34]
                                                   ^
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.

1 个答案:

答案 0 :(得分:0)

将数组投射到bigint[]

db.all("SELECT phase from messages where recipients=ARRAY[12, 34]::bigint[]")