Sequelize:批量插入

Sequelize: insert in bulk(Sequelize:批量插入)

本文介绍了Sequelize:批量插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 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 })

这篇关于Sequelize:批量插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Sequelize:批量插入

基础教程推荐