We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(sequelize)bulkCreate函数中的updateOnDuplicate参数怎么使用?
Sequelize中提供的增删改查相关的函数都能转成原生的sql语句。本篇文章介绍一个不是很常见但是有时候批量插入很方便的函数——bulkCreate。
bulkCreate() - 创建多条记录
bulkCreate(records, [options]) -> Promise.<Array.<Instance>>
批量创建并保存多个实例。
处理成功后,会在回调函数中返回一个包含多个实例的数组。
参数
注意:options.updateOnDuplicate参数中的行键已存在我的理解是:数据库表中现有的记录的唯一索引或者主键如果已经存在,执行更新操作
在MySQL数据库中,如果在insert语句后面带上ON DUPLICATE KEY UPDATE 子句。
看下面的代码:
//对应的node.js代码 db.usernotice.bulkCreate(userNoticeRecord,{validate: true, updateOnDuplicate: ["user_id", "notice_id", "update_time"]}); //生成对应的原生sql语句代码 INSERT INTO `usernotice` (`id`,`user_id`,`notice_id`,`is_read`,`is_zan`,`create_time`) VALUES (NULL,'m_******','345345',false,false,'2019-05-27 17:25:07') ON DUPLICATE KEY UPDATE `user_id`=VALUES(`user_id`),`notice_id`=VALUES(`notice_id`),`update_time`=VALUES(`update_time`);
The text was updated successfully, but these errors were encountered:
No branches or pull requests
(sequelize)bulkCreate函数中的updateOnDuplicate参数怎么使用?
前言:
Sequelize中提供的增删改查相关的函数都能转成原生的sql语句。本篇文章介绍一个不是很常见但是有时候批量插入很方便的函数——bulkCreate。
bulkCreate讲解
官方文档理解
bulkCreate() - 创建多条记录
批量创建并保存多个实例。
处理成功后,会在回调函数中返回一个包含多个实例的数组。
参数
对应原生sql语句讲解
在MySQL数据库中,如果在insert语句后面带上ON DUPLICATE KEY UPDATE 子句。
项目中例子
看下面的代码:
The text was updated successfully, but these errors were encountered: