erick.chali
erick.chali

Reputation: 686

Does django bulk_create possible exceptions

When using Model.objects.bulk_create() if an exception occurs during the insertion does it roll back the entire operation or does it continue with the non-conflicting records, and is there any way to know which records were inserted and which threw an error?

Upvotes: 1

Views: 1607

Answers (1)

Kevin Christopher Henry
Kevin Christopher Henry

Reputation: 48982

If an exception occurs the entire operation will be rolled back. If you look at the source code you'll see that all database operations are wrapped in transaction.atomic().

There's no way of knowing which values caused the conflict. Such information may be available in the database-specific error message, but that's not part of the API.

Note that as of Django 2.2 there will be an ignore_conflicts parameter that will allow you to explicitly control whether the operation will roll back or whether the conflicts will be ignored.

Upvotes: 3

Related Questions