Sequelize:批量插入

2022-01-19 00:00:00 node.js mysql sequelize.js

我正在使用 Node.js、MySQL 和 Sequelize.我想一次将大约 10k 行插入一个表中.该表具有自定义 primaryKey 字段,该字段是手动设置的.数据是从网上下载的,并且是重叠的.

I'm using Node.js, MySQL and Sequelize. I'd like to insert some 10k rows into a table at once. The table has custom primaryKey field, that is being set manually. The data are downloaded from web and are overlapping.

我想要一个 bulkCreate 版本,如果数据中的任何行具有表中已经存在的唯一键,则该版本不会失败.这种事情是在 MySQL 中通过 INSERT ... ON DUPLICATE KEY UPDATE 构造完成的.

I'd like to have a version of bulkCreate that wouldn't fail if any of the rows in data have unique keys that are already present in the table. Such kind of things is done in MySQL via INSERT ... ON DUPLICATE KEY UPDATE construct.

我如何在 Sequelize 中做到这一点?

How do I do it in Sequelize?

推荐答案

将选项对象传递给 bulkCreate 并将 ignoreDuplicates 设置为 true

Pass in an options object to bulkCreate with ignoreDuplicates set to true

bulkCreate([...], { ignoreDuplicates: true })

相关文章