PostgreSQL error 23514 (Check Violation) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.
Read the constraint name from the error detail. If that does not apply, inspect its definition with pg_get_constraintdef() to see the exact rule — the full checklist is below.
SQLSTATE: 23514
Official name: Check Violation
Service: PostgreSQL
A CHECK constraint was violated because a row did not satisfy the constraint expression.
SELECT conname, pg_get_constraintdef(oid)
FROM pg_constraint
WHERE contype = 'c' AND conrelid = 'products'::regclass;
Returns the human-readable CHECK expression so you can see exactly why the row was rejected.
Use validators on model fields to catch invalid data before it hits the DB CHECK.
from django.core.validators import MinValueValidator
class Product(models.Model):
price = models.DecimalField(validators=[MinValueValidator(0)])
Add a CheckConstraint on the model; pair with a Python-side validator for nicer errors.
from sqlalchemy import CheckConstraint
class Product(Base):
price = Column(Numeric, CheckConstraint('price >= 0'), nullable=False)
Most often this happens when inserting a value outside an allowed range (e.g. negative price, age < 0), or when violating a domain or enum membership check.
Read the constraint name from the error detail.
This page documents fixes for: django, sqlalchemy.
Recommendations are editorial — DB Error Reference takes no payment or affiliate fees for tool listings.
This page is based on the official PostgreSQL documentation linked below and adds practical troubleshooting guidance on top.