Handle a uniqueness conflict in Django bulk_create or SQLite UPSERT
Answer: bulk_create() accepts ignore_conflicts and update_conflicts. ignore_conflicts=True tells the database to ignore failed inserts, including duplicate unique values. update_conflicts=True tells the database to update update_fields when a row insertion fails on conflicts.
Django parameters
On PostgreSQL and SQLite, unique_fields that may be in conflict must be provided, in addition to update_fields. Enabling ignore_conflicts disables setting the primary key on each model instance (if the database normally supports it). These options are supported on all but Oracle for ignore_conflicts, and all except Oracle for update_conflicts.
SQLite UPSERT
UPSERT is not standard SQL. SQLite follows the syntax established by PostgreSQL, with generalizations. The syntax in between the ON CONFLICT and DO keywords is called the "conflict target". That target names the uniqueness constraint that will trigger the upsert. Clauses are checked in the order specified. The UPSERT processing happens only for uniqueness constraints. Examples use DO UPDATE SET or DO NOTHING.
| Django | SQLite | |---|---| | ignore_conflicts | Ignore rows that fail constraints | | update_conflicts | Update update_fields when insertion fails on conflicts | | unique_fields | List of unique_fields that may be in conflict | | ON CONFLICT | Conflict target for DO UPDATE or DO NOTHING |
See also
Claims
- id: C1 claim: bulk_create can ignore constraint failures or update fields when an insert conflicts. source: S1 evidence: "tells the database to update update_fields when a row insertion fails on conflicts"
- id: C2 claim: On PostgreSQL and SQLite, unique_fields must be provided with update_conflicts. source: S1 evidence: "a list of unique_fields that may be in conflict must be provided"
- id: C3 claim: SQLite UPSERT uses a conflict target between ON CONFLICT and DO. source: S2 evidence: "The conflict target specifies a uniqueness constraint that will trigger the upsert"
Sources
- [S1] QuerySet API reference. Django docs. https://docs.djangoproject.com/en/5.2/ref/models/querysets/ . License: BSD-3-Clause. Accessed 2026-11-19.
- [S2] UPSERT. SQLite. https://www.sqlite.org/lang_upsert.html . License: Public-Domain. Accessed 2026-11-19.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Django docs, QuerySet API reference; SQLite, UPSERT. BSD-3-Clause; Public-Domain. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Django docs, QuerySet API reference; SQLite, UPSERT. BSD-3-Clause; Public-Domain.
Cite this
iinventedlight. 2026. "Handle a uniqueness conflict in Django bulk_create or SQLite UPSERT." iinventedlight Library, iil:note:cvgm2znh, version of 2026-11-19. https://iinventedlight.com/library/notes/django-on-conflict/. License: CC-BY-4.0.
@misc{iil_note_cvgm2znh,
author = {{iinventedlight}},
title = {{Handle a uniqueness conflict in Django bulk\_create or SQLite UPSERT}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/django-on-conflict/},
note = {Permanent ID iil:note:cvgm2znh. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- django-on-conflict.table.json (table; CC-BY-4.0)
- django-on-conflict.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/django-on-conflict.md.