简体   繁体   中英

PostgreSQL ERROR column does not exist refers to a column value

I have a table projects in postgreSQL that looks like below

id.    project_name.   col3.  col4
1111.   test.          ...    ...

I want to insert a new row for a new id only if the id does not already exist. So I wrote below query

INSERT INTO projects(id, project_name)
VALUES("1234", "Test_Project")
ON CONFLICT (id)
DO NOTHING

But it throws me an error

Query 1 ERROR: ERROR:  column "1234" does not exist
LINE 2: VALUES("1234", "Test_Project_...

Please suggest.

** EDIT**

The id column in the table is a uuid and not unique. There can be multiple rows with the same id. Based on GMBs suggestion, I tried below

INSERT INTO projects(id, project_name)
    VALUES('1234', 'Test_Project')
    ON CONFLICT (create unique index on projects(id))
    DO NOTHING

I get below error with it

Query 1 ERROR: ERROR:  syntax error at or near "create"
LINE 3: ON CONFLICT (create unique index on projects(id...

I am new to this so I am sure missing something obvious.

Use single quotes for literal strings. Double quotes stand for identifiers (such as column names or table names) - hence the error that you are getting:

INSERT INTO projects(id, project_name)
VALUES('1234', 'Test_Project')
ON CONFLICT (id)
DO NOTHING

That said, I would suspect that id is of integer datatype. If so, don't quote it at all:

INSERT INTO projects(id, project_name)
VALUES(1234, 'Test_Project')
ON CONFLICT (id)
DO NOTHING

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM