In my_table, the misc column is jsonb with default value {}. Some rows have misc.names as an array of text values; other rows do not have the misc.names key.
I want to copy that data to a separate names::text[] column, with a command like:
UPDATE my_table SET names = COALESCE(misc->>'names', ARRAY[]::text[])
In other words, "if the row has a list of text values in misc->>'names', put those in the names column, but if misc->>'names' is NULL in that row, put an empty text array in the names column."
However, this fails with:
ERROR: 42804: COALESCE types text and text[] cannot be matched
I don't understand what the error is saying. What am I doing wrong?
misc->'names'"a list of text values" or an actual JSON array? What do you get forjsonb_typeof (misc->'names')? Also (as always) your version of Postgres, please. – Erwin Brandstetter Mar 16 '22 at 22:31