PostgreSQL error code
PostgreSQL error 23514
23514 is the SQLSTATE PostgreSQL reports for check violation, in class 23, Integrity Constraint Violation.
| SQLSTATE | 23514 |
|---|---|
| Condition name | check_violation |
| Class | 23 — Integrity Constraint Violation |
Reproduce it, then fix it
The update would take the balance below zero, which the CHECK constraint forbids. Put the condition in the WHERE clause, and the update skips accounts that can’t afford it. Run the broken query to see the error, then switch to the fixed one. Edit either freely; every run is rolled back.
UPDATE accounts SET balance = balance - 80 WHERE id = 1;
UPDATE accounts SET balance = balance - 80 WHERE id = 1 AND balance >= 80 RETURNING *;