在我的情况下,其他地方(How to push a JSON object to a nested array in a JSONB column)接受的答案无效。
我想将字符串追加到非null JSONB列中的嵌套数组中。如果数组不存在,我想创建它(并添加我的字符串)。更新发生之前,该列的内容将是对象{}
(即不是数组)。
以下只是导致“数据”列中的 null值违反了非null约束约束错误:
update md_ticker
SET data = jsonb_set(data, '{labels}', data -> 'labels' || '"some string"', true)
where id = 74650534
答案 0 :(得分:0)
使用case
检查labels
数组是否存在:
update md_ticker
set data = case
when data ? 'labels' then
jsonb_set(data, '{labels}', data -> 'labels' || '"some string"', true)
else
data || '{"labels": ["some string"]}'
end
where id = 74650534