Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.3k views
in Technique[技术] by (71.8m points)

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

journeypost=# 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...

journeypost=# 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',...

journeypost=# 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?

EDIT:

My schema:

journeypost=# dt user_data.*
           List of relations
  Schema   |   Name    | Type  | Owner 
-----------+-----------+-------+-------
 user_data | user_data | table | user
(1 row)

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...