← All notes

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

Sources

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

Permanent ID: iil:note:cvgm2znh

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.},
}

Markdown

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.

All artifact files (JSON index)

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.