Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inserting custom type array into table column in postgresql-9.4?

I have a custom type "nameage" and a table "namesages".

CREATE TYPE nameage AS(
name_ varchar(50),
age smallint 
);
CREATE TABLE namesages(
id serial,
namesandages nameage[]
);

INSERT INTO namesages(namesandages) VALUES(ARRAY[['john', 24],['david', 38]]::nameage[]);

Why does this query give error?;

ERROR:  malformed record literal: "john"
LINE 1: insert into namesages(namesandages) values(ARRAY[['john', 24...
                                                          ^
DETAIL:  Missing left parenthesis.
********** Error **********
like image 664
tablex Avatar asked Sep 17 '26 22:09

tablex


1 Answers

What you meant to do was this:

INSERT INTO namesages(namesandages) 
VALUES(ARRAY[ROW('john', 24),ROW('david', 38)]::nameage[]);

This creates a one dimensional array of user-defined composite types. I'm not sure what you intended to do when you defined a two-dimensional array...

like image 97
Lukas Eder Avatar answered Sep 19 '26 16:09

Lukas Eder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!