Updating a column by using CASE expression throwing ERROR: column "external_uuid" is of type uuid but expression is of type boolean

Updating a column by using CASE expression throwing ERROR: column "external_uuid" is of type uuid but expression is of type boolean

我有一个 table,我想在其中更新一列 external_uuid 仅当该列已经没有值时:

private const val updateSql = """
  update customer
  set
      external_id = :externalId,
      external_uuid = CASE when external_uuid is null then external_uuid = :externalUuid END,
      name = :name,
      address = :address,
      zip_code = :zipCode,
      zip_area = :zipArea,
      country_code = :countryCode,
      is_deleted = :markedForRemoval
  where is_deleted = false AND (external_uuid = :externalUuid OR (external_id = :externalId AND external_subcustomer = :subCustomer))
"""

但是,如果我进行这样的更新,我会收到错误消息:

 ERROR: column "external_uuid" is of type uuid but expression is of type boolean

如何有条件地在更新时只设置一列?

你的 CASE 表达式 returns 这个布尔表达式的结果:

external_uuid = :externalUuid

可能是 truefalse 并且不能存储在定义为 uuid 的列中,这就是您收到错误的原因。

你应该这样写:

external_uuid = CASE when external_uuid is null then :externalUuid else external_uuid END

或:

external_uuid = coalesce(external_uuid, :externalUuid)