PSQLException:ERROR:operator does not exist:uuid = uuid[]。

5

我正在尝试通过检查uuids是否匹配传递给选择语句的数组中的任何值来从表中提取行。

SELECT * FROM table_name WHERE uuid_column IN ({comma_separated_uuids})

val existingCustomers = fetchRows(
      ctx, generateSQLForTuples(tuples), mapOf("uuids" to customers.map { it["uuid"] as UUID })
  )

下面是generateSQLForTuples函数的代码:

private fun generateSQLForTuples(tuplesList: List<List<String>>):String =
    // language=PostgreSQL
    """
      select id, subcustomer
      from customer
      where uuid in (:uuids)
      union
      select id, subcustomer
      from customer
      where (customer_id, subcustomer) in (${toJdbcTuples(tuplesList)})
    """.trimIndent()

但是,我遇到了错误:

PSQLException: ERROR: operator does not exist: uuid = uuid[]

我在这里做错了什么,如何将包含UUID值的数组传递给选择语句?

7
看起来你正在传递一个数组,因此需要使用where uuid = any(:uuids) - user330315
toJdbcTuples是什么?看起来你正在处理一个列表的列表,而不是元组的列表。 - Bergi
@a_horse_with_no_name 不应该使用 IN 来与数组中的值进行比较吗?它不是和 ANY 一样的作用吗? - Leff
@Leff:如果参数是一个数组,IN 就可以使用值列表,例如 in (1,2,3) - user330315
1
@a_horse_with_no_name 我不确定我是否理解列表和数组之间的区别,map函数返回一个仅包含uuid值的列表,因此它应该与您提到的(1, 2, 3)相同吗? - Leff
3个回答

1

IN运算符不能与ARRAY[]一起使用,它只能与行列一起使用。

您有两个选项来解决这个问题:

  1. 使用unnest()扩展一个数组

例如:column IN (SELECT unnest((ARRAY[uuid1])::uuid[]))

  1. 使用ANY()来处理数组

例如:WHERE column = ANY((ARRAY[uuid1])::uuid[])


0
不要使用"IN"比较符号,应该使用"=",因为如果你正在使用ARRAY[]。

0
我尝试使用IN,但是没有成功。我收到了错误信息:ERROR: operator does not exist: uuid = uuid[]。
我改用WHERE SomeColumn = ANY (@ids)而不是WHERE SomeColumn IN (@ids),结果如预期般工作了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接