【问题标题】:sequelize update transaction续集更新交易
【发布时间】:2016-07-27 23:21:18
【问题描述】:

我在 Nodejs 中使用 sequalize 事务,但我的问题是它不会在事务中获取我的 users 表并更新我的表

return sequelize.transaction(function (t) {
    var Users = objAllTables.users.users();
    return Users.update(updateUser, {
        where: {
            uid: sessionUser.uid,
            status: 'ACTIVE'
        }
    },{ transaction: t }).then(function (result) {

       return Utils.sendVerificationEmail(sessionUser.uid, sessionUser.user_email)
            .then(function(data){
                 data = false;  
                if(data == false){
                        throw new Error('Failed Email');
                }

            });


    }).then(function (result) {
        console.log(result);
        // Transaction has been committed
        // result is whatever the result of the promise chain returned to the transaction callback
    })

}).catch(function(err){
    res.send({message:err.message})
})

控制台:

Executing (ad5247bd-18b8-4c6f-bb30-92744c7a5ac8): START TRANSACTION;
Executing (ad5247bd-18b8-4c6f-bb30-92744c7a5ac8): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
Executing (ad5247bd-18b8-4c6f-bb30-92744c7a5ac8): SET autocommit = 1;
Executing (default): UPDATE `users` SET `username`='edited' WHERE `uid` = 20 AND `status` = 'ACTIVE'
Executing (ad5247bd-18b8-4c6f-bb30-92744c7a5ac8): ROLLBACK;

正如您在控制台更新查询中所见,事务已用完

【问题讨论】:

  • 我想在我的控制台中打印相同的执行。锄头这样做?
  • @shumanachowdhury 您需要在连接到数据库时启用登录 sequelize 选项。见this

标签: node.js transactions sequelize.js


【解决方案1】:

transaction 密钥必须在options

return Users.update(updateUser, {
        where: {
            uid: sessionUser.uid,
            status: 'ACTIVE'
        },
        transaction: t     //second parameter is "options", so transaction must be in it
    })

【讨论】:

  • 如果我们使用sequelize的destroy方法呢?即 users.destroy()
  • @AhmerSaeed destroy 只需要 1 个参数,您必须传递其中的所有选项。 users.destroy({ where: { id: 57 }, transaction: t })
  • 对于任何搜索此内容的人(因为我偶尔会这样做),可以在此处找到更新功能的 sequelize 文档(因为在我看来 Google 并不总是能找到它):sequelize.org/master/class/lib/…
猜你喜欢
  • 1970-01-01
  • 2017-04-03
  • 2020-05-15
  • 1970-01-01
  • 2015-05-17
  • 1970-01-01
  • 1970-01-01
  • 2013-09-03
相关资源
最近更新 更多