Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django bulk_create with ignore rows that cause IntegrityError?

I am using bulk_create to loads thousands or rows into a postgresql DB. Unfortunately some of the rows are causing IntegrityError and stoping the bulk_create process. I was wondering if there was a way to tell django to ignore such rows and save as much of the batch as possible?

like image 946
Meitham Avatar asked Sep 16 '12 21:09

Meitham


1 Answers

This is now possible on Django 2.2

Django 2.2 adds a new ignore_conflicts option to the bulk_create method, from the documentation:

On databases that support it (all except PostgreSQL < 9.5 and 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. Enabling this parameter disables setting the primary key on each model instance (if the database normally supports it).

Example:

Entry.objects.bulk_create([     Entry(headline='This is a test'),     Entry(headline='This is only a test'), ], ignore_conflicts=True) 
like image 195
Cesar Canassa Avatar answered Sep 20 '22 03:09

Cesar Canassa