续集:批量插入 [英] Sequelize: insert in bulk

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

问题描述

我正在使用Node.js,MySQL和Sequelize.我想一次在表格中插入约1万行.该表具有自定义的primaryKey字段,该字段是手动设置的.数据是从Web下载的,并且是重叠的.

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版本,如果数据中的任何行具有表中已经存在的唯一键,则该版本不会失败.这类事情是通过INSERT ... ON DUPLICATE KEY UPDATE构造在MySQL中完成的.

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?

推荐答案

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

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

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

这篇关于续集:批量插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆