Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequelize update transaction

I am using sequalize transaction in Nodejs,but my problem is that it don't take my users table in Transaction and update my table

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})
})

CONSOLE:

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;

As you can see in the console update query run out of the transaction

like image 940
Wasiq Muhammad Avatar asked Apr 07 '16 07:04

Wasiq Muhammad


People also ask

How do you update data in Sequelize?

First, you get an instance of the Model by calling the create() or findOne() method. Once you have the instance, you call the set() method to change the values of the instance. Then, you need to call the save() method on the instance to persist the changes to your table row.


1 Answers

transaction key must be in options:

return Users.update(updateUser, {
        where: {
            uid: sessionUser.uid,
            status: 'ACTIVE'
        },
        transaction: t     //second parameter is "options", so transaction must be in it
    })
like image 163
Shaharyar Avatar answered Sep 28 '22 00:09

Shaharyar