Django - SQL bulk get_or_create possible? Django - SQL bulk get_or_create possible? sql sql

Django - SQL bulk get_or_create possible?

This post may be of use to you:

Note that the answer recommends using the commit_on_success decorator which is deprecated. It is replaced by the transaction.atomic decorator. Documentation is here:


from django.db import transaction@transaction.atomicdef lot_of_saves(queryset):    for item in queryset:        modify_item(item)

As of Django 2.2, bulk_create has an ignore_conflicts flag. Per the docs:

On databases that support it (all but Oracle), setting the ignore_conflicts parameter to True tells the database to ignore failure to insert any rows that fail constraints such as duplicate unique values

If I understand correctly, "get_or_create" means SELECT or INSERT on the Postgres side.

You have a table with a UNIQUE constraint or index and a large number of rows to either INSERT (if not yet there) and get the newly create ID or otherwise SELECT the ID of the existing row. Not as simple as it may seem on the outside. With concurrent write load, the matter is even more complicated.

And there are various parameters that need to be defined (how to handle conflicts exactly):