skip to Main Content

I am working in Postgres. I have the below composite type

CREATE TYPE negotiation.diff_type AS 
(
    operation int,
    content text
);

This is my table

CREATE temp TABLE temp_input 
(
    indication_request_id int, 
    para_id int, 
    line_number int, 
    document_id int, 
    modified_content text, 
    created_by text, 
    diffs diff_type[]
);

In the table I am using diffs composite type.

Below I am inserting data into the table.

INSERT INTO temp_input (myid, para_id, line_number, document_id,modified_content,created_by, diffs)
VALUES (20,2893,10,18,'my content','user1', '{(1,"DDD")"}');

This I get this error:

ERROR: malformed array literal: "{(1,"DDD")"}"

2

Answers


  1. You need to surround each element of the array with quotes, and then escape the quotes inside the complex type’s literal:

    INSERT INTO temp_input
    (indication_request_id, para_id, line_number, document_id,modified_content,created_by, diffs)
    VALUES (20,2893,10,18,'my content','user1', '{"(1,"DDD")"}');
    

    SQLFiddle Demo

    Login or Signup to reply.
  2. This is how you create a literal holding an array of negotiation.diff_type:

    array['(1,abc)','(2,def)']::negotiation.diff_type[]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search