I'm attempting to do a django bulk_create
for a list of objects. However, every so often one of the objects in the bulk_create
already exists (it's primary key has already been allocated) so an exception is raised and no objects get created. To fix this I added a loop over each of the new objects to test if they exist prior to the bulk_create
call but this feels very inefficient. Is there way to accomplish the same result in fewer queries to the database?
CodePudding user response:
Get all records from your database once. Then check if there are any duplicates. Eliminate them and do bulk create.
Depends on what database you are using. If for example you are using Oracle, then maybe there are options with custom PL/SQL procedures you can create. I don't know PL/SQL though, I am just sure that if it is a language, then there must be options...
CodePudding user response:
I think this is feasible directly with the Django Queryset API if you are in a recent Django version.
From Django 2.2, bulk_create
includes a parameter called ignore_conflicts
that would continue the execution of the bulk_create
when an error is found.
This saves you having to do any extra queries. You can check the docs here: https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.bulk_create