Home > database >  Updating a column by using CASE expression throwing ERROR: column "external_uuid" is of ty
Updating a column by using CASE expression throwing ERROR: column "external_uuid" is of ty

Time:12-22

I have a table where I would like to do update a column external_uuid only if that column already doesn't have a value:

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))
"""

But, if I do an update like that I get an error:

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

How can I conditionally set only one column on an update?

CodePudding user response:

Your CASE expression returns the result of this boolean expression:

external_uuid = :externalUuid

which may be true or false and can't be stored in a column defined as uuid and this is why you get the error.

You should write it like this:

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

or:

external_uuid = coalesce(external_uuid, :externalUuid)
  • Related