PostgreSQL error code
PostgreSQL error 23503
23503 is the SQLSTATE PostgreSQL reports for foreign key violation, in class 23, Integrity Constraint Violation.
| SQLSTATE | 23503 |
|---|---|
| Condition name | foreign_key_violation |
| Class | 23 — Integrity Constraint Violation |
Reproduce it, then fix it
There is no customer 999 for the order to point to. Deleting a customer who still has orders raises the same error from the other direction. Run the broken query to see the error, then switch to the fixed one. Edit either freely; every run is rolled back.
INSERT INTO orders (id, customer_id, status) VALUES (100, 999, 'pending');
INSERT INTO orders (id, customer_id, status) VALUES (100, 1, 'pending') RETURNING *;