Django save fails on PostgreSQL: null value in a column violates a not-null constraint
django.db.utils.IntegrityError: null value in column "price" of relation "shop_product" violates not-null constraintShort 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.
Fix it fast
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"2Match what you see, then jump to its fix
- 1
The 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
The 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
Raw 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
- 1
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.py2Fix
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 migrate3Verify
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
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.py2Fix
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 checkConfirmed by docs.djangoproject.com
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 migrate3Verify
Insert a row with SQL that omits the column.
python manage.py dbshellConfirmed by docs.djangoproject.com
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:
- stegnor.comMy company site: 804 pages from a Python and Jinja generator, with 15 free browser tools.
- MenuloA restaurant discovery platform in Germany, kept current by a Python ingestion pipeline.
- ShopGoAn e-commerce aggregator with a Python scraping engine behind its catalog, past 10K Google Play downloads.
Related fixes
Databases
python manage.py migrate finishes but the tables for my app are missing
relation "myapp_book" does not exist
migrate only applies migration files that exist.
3 causes, each with a fixDatabases
Django migration on PostgreSQL stops with 'pending trigger events'
django.db.utils.OperationalError: cannot ALTER TABLE "mytable" because it has pending trigger events
PostgreSQL runs a Django migration in one transaction.
2 causes, each with a fixDatabases
psycopg2 cannot connect: connection refused on port 5432
Is the server running on that host and accepting TCP/IP connections?
Connection refused means nothing accepted the connection at the address psycopg2 tried.
3 causes, each with a fixDatabases
psycopg2 cannot resolve the host name db in Docker Compose
could not translate host name "db" to address
Compose registers each service in its own DNS under the service name, but only containers on the same Compose network can look it up.
4 causes, each with a fixDatabases
InFailedSqlTransaction: commands ignored until end of transaction block
SQLSTATE 25P02 (in_failed_sql_transaction)
Some earlier statement in the same transaction failed, and PostgreSQL ignores everything after it until you roll back.
3 causes, each with a fixDatabases
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