Skip to content

CHECK Constraint Violation

Error message

                new row for relation violates check constraint
              

Quick summary

You tried to insert or update a value that fails a CHECK constraint — a custom condition like `price > 0` or `age >= 18`.

Why this happens

  • Inserting a negative price when the constraint requires price > 0

  • Inserting an invalid age, date range, or other out-of-bounds value

  • Application logic does not validate data before sending to the database

Minimal example

✗ Broken code
INSERT INTO products (id, name, price) VALUES (1, 'Widget', -9.99);
-- But price has CHECK (price > 0)
✓ Fixed code
INSERT INTO products (id, name, price) VALUES (1, 'Widget', 9.99);

A negative price violates the CHECK constraint. Using a positive price satisfies it.

How to diagnose

  • Read the error to identify which CHECK constraint was violated

  • Check the constraint definition: \d table_name in psql

  • Verify the value you are trying to insert against the constraint condition

How to fix

  • Provide a value that satisfies the CHECK condition

  • Update the constraint if the business rule has changed

  • Validate data in your application before inserting

How to prevent

  • Validate data in your application using the same rules as the CHECK constraint

  • Use form validation to prevent invalid input

  • Document CHECK constraints and their business meaning

Related Resources

Related Glossary

Related Lessons

← Back to language