Django bulk_create 忽略导致 IntegrityError 的行?

标签 django postgresql bulk-load

我正在使用 bulk_create 将数千或行加载到 postgresql 数据库中。不幸的是,某些行导致了 IntegrityError 并停止了 bulk_create 进程。我想知道是否有办法告诉 django 忽略这些行并尽可能多地保存批处理?

最佳答案

现在可以在 Django 2.2 上实现

Django 2.2 为 bulk_create 方法添加了一个新的 ignore_conflicts 选项,来自 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).

例子:

Entry.objects.bulk_create([
    Entry(headline='This is a test'),
    Entry(headline='This is only a test'),
], ignore_conflicts=True)

关于Django bulk_create 忽略导致 IntegrityError 的行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12451053/

相关文章:

sql - 在查询中连接复合外键/主键列的表

postgresql - web2py : select clause to get first x number of records

mysql - 将 MySQL 的 LOAD DATA INFILE 与 node.js 一起使用?

django - 使用 django CSRF 中间件和返回 JsonResponse 的 View

python - 带有嵌套列表的字典 TypeError : string indices must be integers

node.js - Express.js 如何在登录时和之后使用安全性

sql - 收集索引的统计信息或删除创建?

Python/Django - 初始/默认管理页面

python - 删除级联的 Django 在 PostgreSQL 中没有产生正确的约束

elasticsearch - elasticsearch批量加载中的@param是什么