[英]PostgreSQL - how to insert into array of composite type
CREATE TYPE myenum AS ENUM ('title', 'link', 'text');
CREATE TYPE struct AS (
type myenum,
content varchar(300)
);
CREATE TABLE "mytable" (
id serial PRIMARY KEY,
array struct[]
);
INSERT INTO "mytable" VALUES (
DEFAULT,
'{"(\"title\",\"my title\")","(\"link\",\"www.google.com\")"}'
);
INSERT INTO "mytable" VALUES (
DEFAULT,
ARRAY['(\"title\",\"my title\")', '(\"link\",\"www.google.com\")']
);
INSERT INTO "mytable" VALUES (
DEFAULT,
ARRAY[('title','my title'), ('link','www.google.com')]
);
我想插入一些數據,我嘗試了很多 forms 但它們都無法插入成功,並且有一些錯誤消息
error: malformed array literal: "{"("title","my title")","("link","www.google.com")"}"
error: malformed array literal: "{("title","my title"), ("link","www.google.com")}"
error: column "placeholder_array" is of type placeholder_struct[] but expression is of type text[]
error: column "placeholder_array" is of type placeholder_struct[] but expression is of type record[]
需要幫助pppp,這對我來說太難了,太麻煩了,非常感謝!!!!
1)。 不要調用您的列array
,這是一個關鍵字。
2)。 使用::struct
將您的元組轉換為該數據類型。
CREATE TYPE myenum AS ENUM ('title', 'link', 'text');
CREATE TYPE struct AS (
type myenum,
content varchar(300)
);
CREATE TABLE mytable (
id serial PRIMARY KEY,
val_array struct[]
);
INSERT INTO mytable VALUES (
DEFAULT,
ARRAY[('title','my title')::struct, ('link','www.google.com')::struct]
);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.