Nodejs续集批量更新 [英] Nodejs sequelize bulk upsert

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

问题描述

有没有办法在 sequelize 中进行批量 upsert.另外,我可以指定用于检查重复项的键吗?

Is there a way of doing bulk upsert in sequelize. Also, can I specify which keys to use for checking for duplicates?

我尝试了以下方法,但没有成功:

I tried following but it didn't work:

Employee.bulkCreate(data, {
    updateOnDuplicate: true
});

虽然批量创建工作正常.上面的语句总是在数据库中创建新条目.

Bulk creation works fine though. Above statement always creates new entries in the DB.

推荐答案

来自官方sequelizejs 参考.

可以使用带有 updateOnDuplicate 选项的 bulkCreate 来完成.

It can be done using bulkCreate with the updateOnDuplicate option.

例如这样:

Employee.bulkCreate(dataArray, 
    {
        fields:["id", "name", "address"] ,
        updateOnDuplicate: ["name"] 
    } )

updateOnDuplicate 是一个字段数组,当主键(或可能是唯一键)匹配行时将更新这些字段.确保您的模型和 dataArray 中至少有一个唯一字段(比如说 id)用于 upsert.

updateOnDuplicate is an array of fields that will be updated when the primary key (or may be unique key) match the row. Make sure you have at least one unique field (let say id) in your model and in the dataArray both for upsert.

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

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