1
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')]
        );

i want to insert some data , and i tried many forms but they all can't insert success, and there are some error messages

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[]

need helppppp! that's so hard and messy for me, thank you very much!!!!

1 Answer 1

2

1). Don't call your column array, that's a keyword.

2). Use ::struct to cast your tuples to that datatype.

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]
        );

db<>fiddle demo

Sign up to request clarification or add additional context in comments.

1 Comment

the first question is because i just show it clearly, its name is not array actually, AND the second also MOST IMPORTANT , it does works!! thank you!!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.