gpt4 book ai didi

sql - Postgres : push unique object into an array of jsonb

转载 作者:行者123 更新时间:2023-12-03 22:34:46 25 4
gpt4 key购买 nike

我有一个 jsonb 数组 appliances 如果对象不存在,我试图将其推送到其中。我正在尝试使用此查询

update home
set appliances = appliances ||
'{"name": "Television", "uuid": "f6847317-d6fc-476f-8439-3a81f169e9ad"}'
where uuid = 'f5018a28-fd5f-43df-9d09-4e73da448823'
and not
'{"name": "Television", "uuid": "f6847317-d6fc-476f-8439-3a81f169e9ad"}'::jsonb
< @ appliances;

但它抛出一个错误:

ERROR: operator does not exist: jsonb <@ jsonb[]


appliances 列的数据类型为 jsonb[] 。我怎样才能使这个查询正确?

最佳答案

PostgreSQL 数组上的 <@ 运算符和 || 运算符需要两个操作数都是数组,而不仅仅是其中之一。

update home
set appliances = appliances ||
ARRAY['{"name": "Television", "uuid": "f6847317-d6fc-476f-8439-3a81f169e9ad"}'::jsonb]
where uuid = 'f5018a28-fd5f-43df-9d09-4e73da448823'
and not
ARRAY['{"name": "Television", "uuid": "f6847317-d6fc-476f-8439-3a81f169e9ad"}'::jsonb]
<@ appliances;

关于sql - Postgres : push unique object into an array of jsonb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62356172/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com