PostgreSQL error code
PostgreSQL error 23505
23505 is the SQLSTATE PostgreSQL reports for unique violation, in class 23, Integrity Constraint Violation.
| SQLSTATE | 23505 |
|---|---|
| Condition name | unique_violation |
| Class | 23 — Integrity Constraint Violation |
Reproduce it, then fix it
A product with id 1 already exists, and the primary key allows each id once. ON CONFLICT turns the insert into an update when the row is already there. Run the broken query to see the error, then switch to the fixed one. Edit either freely; every run is rolled back.
INSERT INTO products (id, name) VALUES (1, 'Webcam');
INSERT INTO products (id, name) VALUES (1, 'Webcam') ON CONFLICT (id) DO UPDATE SET name = excluded.name RETURNING id, name;