Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot INSERT: ERROR: array value must start with "{" or dimension information

INSERT INTO user_data.user_data (username,randomint)
VALUES ('mahman',1);
ERROR:  array value must start with "{" or dimension information
LINE 1: ... user_data.user_data (username,randomint) VALUES ('mahman...
INSERT INTO user_data.user_data (username,randomint)
VALUES {'mahman',1};
ERROR:  syntax error at or near "{"
LINE 1: ...O user_data.user_data (username,randomint) VALUES {'mahman',...
INSERT INTO user_data.user_data (username,randomint)
VALUES (2,{'mahman',1});
ERROR:  syntax error at or near "{"
LINE 1: ...ser_data.user_data (username,randomint) VALUES (2,{'mahman',...

The above varying INSERT statements fail in PostgreSQL. What is it that I am doing wrong?

My schema:

journeypost=# \dt user_data.*
           List of relations
  Schema   |   Name    | Type  | Owner 
-----------+-----------+-------+-------
 user_data | user_data | table | user

My table:

journeypost=# \d user_data.user_data
           Table "user_data.user_data"
  Column   |          Type           | Modifiers 
-----------+-------------------------+-----------
 username  | character varying(50)[] | not null
 randomint | integer                 | 
Indexes:
    "UsernameUnique" PRIMARY KEY, btree (username)
like image 577
Noob Doob Avatar asked Jul 20 '15 14:07

Noob Doob


1 Answers

Your column username seems to be an array type, so the literal 'mahman' is not valid input for it.

It would have to be '{mahman}':

INSERT INTO user_data.user_data (username,randomint)
VALUES ('{mahman}',1);

(Or make it a plain varchar column or text column instead.)

Update confirms it: character varying(50)[] is an array of character varying(50).

About array literal / array constructor:

  • Pass array from node-postgres to plpgsql function
  • How to pass custom type array to Postgres function
like image 64
Erwin Brandstetter Avatar answered Nov 15 '22 10:11

Erwin Brandstetter