Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Databases and Redis

Django save fails on PostgreSQL: null value in a column violates a not-null constraint

Errordjango.db.utils.IntegrityError: null value in column "price" of relation "shop_product" violates not-null constraint

Short answer

A model field is NOT NULL in the database unless it has null=True. The error means an INSERT or UPDATE sent NULL for such a column. Supply a value, give the field a default, make it nullable with null=True and a migration, or for text fields store an empty string. Rows written outside Django need a database-level default.

  • Applies to Django 5.2, PostgreSQL 16, Python 3.12
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Read the column name in the error, find the field in your model, and look at its null and default options.

    python manage.py sqlmigrate shop 0001 | grep -i "price"
  2. 2Match what you see, then jump to its fix

    1. 1The failing column is a field you never set, or set from an optional form value or API fieldThe code saves None into a field that does not allow NULLFix
    2. 2The failing column is a CharField or TextField and the value came from JSON, a form or a CSVA text field receives None instead of an empty stringFix
    3. 3Raw SQL, another service or a data import fails, while saving through Django worksRows are inserted outside the Django ORM and the default exists only in PythonFix

What does the error look like?

  • django.db.utils.IntegrityError: null value in column "price" of relation "shop_product" violates not-null constraint

Cause 1 of 3

The code saves None into a field that does not allow NULL

1Check

Fields default to null=False and have no default unless you add one. Optional input that is missing arrives as None.

grep -n "price" shop/models.py

2Fix

Either always provide a value, add a default, or make the column nullable and migrate. blank=True only relaxes form validation and does not change the column.

price = models.DecimalField(max_digits=8, decimal_places=2, default=0)
# or, when empty is meaningful:
price = models.DecimalField(max_digits=8, decimal_places=2, null=True, blank=True)

python manage.py makemigrations
python manage.py migrate

3Verify

Save an instance without the value and confirm it no longer raises.

python manage.py shell -c "from shop.models import Product; Product.objects.create(name='x')"

Confirmed by docs.djangoproject.com

Did this fix it?

Cause 2 of 3

A text field receives None instead of an empty string

1Check

For string fields the Django convention is an empty string, not NULL, as the no-data state, so these fields normally have null=False.

grep -n "CharField\|TextField" shop/models.py

2Fix

Convert None to an empty string before saving, and mark the field blank=True if forms may leave it empty.

subtitle = models.CharField(max_length=200, blank=True)

product.subtitle = data.get("subtitle") or ""
product.save()

3Verify

Save again with the value missing.

python manage.py check

Confirmed by docs.djangoproject.com

Did this fix it?

Cause 3 of 3

Rows are inserted outside the Django ORM and the default exists only in Python

1Check

Field.default is applied by Django when a new instance is created. It is not a column default in the database, so an INSERT that skips the column gets NULL.

python manage.py sqlmigrate shop 0001 | grep -i "default"

2Fix

On Django 5.0 or later set db_default so the database computes the value, then migrate.

quantity = models.IntegerField(db_default=1)

python manage.py makemigrations
python manage.py migrate

3Verify

Insert a row with SQL that omits the column.

python manage.py dbshell

Confirmed by docs.djangoproject.com

Did this fix it?

How do I stop it coming back?

  • Decide null, blank and default together when you add a field, and write it in the migration.
  • Validate incoming API or CSV data before calling save, so a missing value becomes an error message, not a database error.
  • Test saves with optional fields omitted, on PostgreSQL rather than only on SQLite.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Databases

psycopg2 hangs with no timeout: connect, query and dead connections

psycopg2.errors.QueryCanceled (SQLSTATE 57014) after you set a statement timeout

Neither psycopg2 nor PostgreSQL sets a time limit by default, so a stuck connect, a slow query, a lock wait or a silently dead network link can wait forever.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.