Django bulk_create with ignore rows that cause IntegrityError?

后端 未结 6 1719
孤城傲影
孤城傲影 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 03:02

    Late answer for pre Django 2.2 projects :

    I ran into this situation recently and I found my way out with a seconder list array for check the uniqueness.

    In my case, the model has that unique together check, and bulk create is throwing Integrity Error exception because of the array of bulk create has duplicate data in it.

    So I decided to create checklist besides bulk create objects list. Here is the sample code; The unique keys are owner and brand, and in this example owner is an user object instance and brand is a string instance:

    create_list = []
    create_list_check = []
    for brand in brands:
        if (owner.id, brand) not in create_list_check:
            create_list_check.append((owner.id, brand))
            create_list.append(ProductBrand(owner=owner, name=brand))
    
    if create_list:
        ProductBrand.objects.bulk_create(create_list)
    

提交回复
热议问题