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:批量插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)
SQL/MySQL: split a quantity value into multiple rows by date(SQL/MySQL:按日期将数量值拆分为多行)