Nodejs 续集批量更新插入

编程入门 行业动态 更新时间:2024-10-14 00:28:58
本文介绍了Nodejs 续集批量更新插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

有没有办法在 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 续集批量更新插入

本文发布于:2023-10-27 10:36:23,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533118.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:续集   批量   Nodejs

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!