【问题标题】:How to update a jsonb column and put text array如何更新 jsonb 列并放置文本数组
【发布时间】:2018-05-17 02:22:51
【问题描述】:

我有这样的 jsonb 列:

{name: "Toby", occupation: "Software Engineer", interests: ""}

现在,我需要更新行并将像 ['Volleyball', 'Football', 'Swim'] 这样的文本数组放入 interests 字段。 到目前为止我尝试过的:

UPDATE users SET data = jsonb_set(data, '{interests}', ARRAY['Volleyball', 'Football', 'Swim'], true) WHERE id=84;

data 是 jsonb 列

但是它返回一个错误:

错误:函数 jsonb_set(jsonb, unknown, integer[], boolean) 没有 存在

提示:没有函数匹配给定的名称和参数类型。你 可能需要添加显式类型转换。

PS:

我正在使用 PostgreSQL 10

【问题讨论】:

    标签: arrays json postgresql


    【解决方案1】:

    第三个参数也需要是 JSONB 类型。

    UPDATE users SET data = jsonb_set(data, '{interests}', '["Volleyball", "Football", "Swim"]'::jsonb, true) WHERE id=84;
    

    这也可以,这更接近您使用ARRAY 的示例:

    UPDATE users SET data = jsonb_set(data, '{interests}', to_jsonb(array['Volleyball', 'Football', 'Swim']), true) WHERE id=84
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-06
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-22
      相关资源
      最近更新 更多