PostgreSQL error 23502 (Not Null Violation) explained: what it means, why it happens, and how to fix it — with copy-paste code examples.
Read the error for the exact column that received NULL. If that does not apply, provide an explicit non-null value in the INSERT or fix the application mapping — the full checklist is below.
SQLSTATE: 23502
Official name: Not Null Violation
Service: PostgreSQL
A NOT NULL constraint was violated by inserting or updating a column with a NULL value.
UPDATE users SET email = 'unknown@example.com' WHERE email IS NULL;
ALTER TABLE users ALTER COLUMN email SET NOT NULL;
Never add NOT NULL to a column containing NULLs; backfill first or the constraint fails for existing rows.
Mark fields as required (non-optional) in schema.prisma; the client will type-check before sending NULL.
model User {
id Int @id @default(autoincrement())
email String // required, cannot be null
}
Use null=False with a default, or validate in clean() before save().
class MyModel(models.Model):
email = models.EmailField(null=False, default='')
Declare nullable=False on the column; the ORM rejects None before reaching the DB.
class User(Base):
__tablename__ = 'users'
email = Column(String, nullable=False)
Most often this happens when omitting a required column in an INSERT statement, or when passing NULL/undefined from the application for a NOT NULL column.
Read the error for the exact column that received NULL.
This page documents fixes for: prisma, 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.