Django bulk_create with ignore rows that cause IntegrityError?

后端 未结 6 1715
孤城傲影
孤城傲影 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:47

    Or 5. Divide and conquer

    I didn't test or benchmark this thoroughly, but it performs pretty well for me. YMMV, depending in particular on how many errors you expect to get in a bulk operation.

    def psql_copy(records):
        count = len(records)
        if count < 1:
            return True
        try:
            pg.copy_bin_values(records)
            return True
        except IntegrityError:
            if count == 1:
                # found culprit!
                msg = "Integrity error copying record:\n%r"
                logger.error(msg % records[0], exc_info=True)
                return False
        finally:
            connection.commit()
    
        # There was an integrity error but we had more than one record.
        # Divide and conquer.
        mid = count / 2
        return psql_copy(records[:mid]) and psql_copy(records[mid:])
        # or just return False
    

提交回复
热议问题