【问题标题】:Insert into jsonb[] column插入 jsonb[] 列
【发布时间】:2022-01-14 16:57:55
【问题描述】:

有了这张桌子

CREATE TABLE IF NOT EXISTS actions (
    action_id UUID NOT NULL DEFAULT uuid_generate_v4(),
    inputs JSONB[] NOT NULL DEFAULT ARRAY[]::JSONB[]
)

我正在尝试插入此数据

INSERT INTO actions (
    action_id,
    inputs
)
VALUES (
    '41fc94af-2f4e-424f-acde-641bb63f4b82',
    array['{"type":"string"}{"displayName":"Base OAuth URL","type":"string","description":"Base OAuth URL"}']::jsonb[]
);

但得到错误

ERROR:  invalid input syntax for type json
LINE 7:                                 array['{"type":"string"}{"di...
                                              ^
DETAIL:  Expected end of input, but found "{".
CONTEXT:  JSON data, line 1: {"type":"string"}{...

这也行不通

INSERT INTO actions (
    action_id,
    inputs
)
VALUES (
    '41fc94af-2f4e-424f-acde-641bb63f4b82',
    [{"type":"string"}{"displayName":"Base OAuth URL","type":"string","description":"Base OAuth URL"}]::jsonb[]
);

【问题讨论】:

  • 您正在混合 Postgres arrayjsonb 类型。你真的想要 jsonb 对象的 Postgres array 还是存储为 jsonb 的 JSON 对象数组?
  • 避免jsonb[],它很难索引并且处理起来很混乱
  • 我同意另外两个。 jsonb[] 在我看来毫无意义

标签: postgresql jsonb


【解决方案1】:

我没有得到你的意图。可以肯定的是,您在 array[..] 中写入的内容既不是单个 json,也不是 json 数组。

如果{"type":"string"}{"displayName":"Base OAuth URL","type":"string","description":"Base OAuth URL"} 是两个不同的对象,那么你必须这样写:

array[
    '{"type":"string"}',
    '{"displayName":"Base OAuth URL","type":"string","description":"Base OAuth URL"}'
    ]::jsonb[]

【讨论】:

  • 就是这样。错误的 json。感谢您指出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
  • 2020-07-24
  • 1970-01-01
  • 2018-12-10
  • 2020-03-04
相关资源
最近更新 更多