Nodejs 续集批量更新插入 [英] Nodejs sequelize bulk upsert
问题描述
有没有办法在 sequelize 中进行批量更新插入.另外,我可以指定使用哪些键来检查重复项吗?
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屋!