我正在Postgres中使用JSONB,并试图了解如何正确地执行对plpgsql中的JSON属性的赋值。
这个查询片段报告了一个语法错误,因为在赋值中使用了前面的括号,但是,我确信这个语法是引用plpgsq中的JSON对象所必需的:
IF (NEW."data")->>'custom' IS NULL THEN
(NEW."data")->>'custom' := 0;
END IF;这是在postgresql触发器中,因此NEW是提供给新数据库记录的变量。
有人能建议给JSON(B)属性赋值的正确技术吗?
发布于 2016-02-08 18:07:24
在Postgres 9.5中,使用jsonb_set()函数非常简单
if new.data->>'custom' is null then
new.data = jsonb_set(new.data::jsonb, '{custom}', '0'::jsonb);
end if;jsonb_set()在Postgres 9.4中没有,因此问题更加复杂:
if new.data->>'custom' is null then
new.data = (
select json_object_agg(key, value)
from (
select key, value
from jsonb_each(new.data)
union
values ('custom', '0'::jsonb)
) s
);
end if;https://stackoverflow.com/questions/35275869
复制相似问题