我正在尝试将一个新的整数值添加到postgres表中现有的整数数组中。我在这里使用array_append方法,但得到"Cannot cast type jsonb to integer[]“错误。我的更新查询如下所示。
注意--我只是为了学习的目的而使用它,所以我故意将using _Note保存为整数。

update querytesting set jsondoc=jsondoc||jsonb_build_object(jsondoc->'PhoneNumber',array_append(((jsondoc->'PhoneNumber')::int[]),'6789')) where id=1;
发布于 2021-09-30 06:17:47
您必须提取数组,附加新的数字,用新的数组创建一个新的JSON并存储它。如果您将数据存储在规范化的数据模型中而不是JSON中,那么就不需要这个冗长而低效的过程。
UPDATE querytesting
SET jsondoc = jsonb_set(
jsondoc,
'{PhoneNumber}',
jsondoc -> 'PhoneNumber' || JSONB '[6789]'
);
WHERE id=1;https://stackoverflow.com/questions/69386261
复制相似问题