在 Postgres 脚本中将 uuid 插入 json 列的正确格式是什么?

问题描述 投票:0回答:1

我想将 Postgres 脚本中 UUID 类型的变量插入到“jsonb”类型的列中。 表格看起来像:

create table entity ( id bigint primary key, tags jsonb );

脚本看起来像:

DO $$
DECLARE
  tagid1 UUID := '77170ed4-4d39-4799-ad22-750e57a11833';
  tagid2 UUID := '890b96ad-c595-4b6e-9e49-cad234190fda';

  insert into entity( id, tags ) VALUES ( 1, array_to_json(tagid1::uuid[]) );
  insert into entity( id, tags ) VALUES ( 2, array_to_json(tagid1, tagid2::uuid[]) );
END $$

此脚本中止并显示“错误:无法将类型 uuid 转换为类型 uuid[]”

postgresql uuid jsonb
1个回答
0
投票

如果您想要一个数组,请使用数组构造函数或文字。

SELECT array_to_json(array['77170ed4-4d39-4799-ad22-750e57a11833', '890b96ad-c595-4b6e-9e49-cad234190fda']);

SELECT array_to_json('{77170ed4-4d39-4799-ad22-750e57a11833,890b96ad-c595-4b6e-9e49-cad234190fda
clwrota_test'> }');

注意 - 如果您的应用程序首先提供数组,您可能不需要其中任何一个 - 只需

array_to_json(<placeholder>)

注意 - 您可能需要“jsonb”而不是“json” - 有关详细信息,请参阅手册。

© www.soinside.com 2019 - 2024. All rights reserved.