Django bulk_create with ignore rows that cause IntegrityError?

后端 未结 6 1705
孤城傲影
孤城傲影 2020-12-03 02:46

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

6条回答
  •  抹茶落季
    2020-12-03 02:55

    One quick-and-dirty workaround for this that doesn't involve manual SQL and temporary tables is to just attempt to bulk insert the data. If it fails, revert to serial insertion.

    objs = [(Event), (Event), (Event)...]
    
    try:
        Event.objects.bulk_create(objs)
    
    except IntegrityError:
        for obj in objs:
            try:
                obj.save()
            except IntegrityError:
                continue
    

    If you have lots and lots of errors this may not be so efficient (you'll spend more time serially inserting than doing so in bulk), but I'm working through a high-cardinality dataset with few duplicates so this solves most of my problems.

提交回复
热议问题