Reputation: 141
` User.update({
role: 'user'
}, {
where: {
email: email
}
}).then(function(result) {
//Returned value: either [0] or [1] i.e. record is updated or not
});
`
This is sequelize update query where User is model (user's role is updated by email). Its working fine but I want that using update query, I will get user's data as well. Is it possible to get it without using another query ?
Upvotes: 5
Views: 6383
Reputation: 161
You can get the updated value by calling this function model.reload()
. Maybe will not work on older versions of Sequelize. Link to docs.
Upvotes: 1
Reputation: 63
If you are using postgresql then you can set returning true after where clause
where: { socketID: socket.id },
returning: true,
and and if you are using other db like mysql as in my case then you cant to it in a single hit you have to use anther query like below
async function PersonalDetail(req, res) {
var Email = req.body.email;
const user = await User.findOne({ where: { EmailAddress: Email, IsActive: true } });
if (user != null) {
user.UpdateDate = new Date();
user.EmplomentID = req.body.EmplomentID;
user.LivingID = req.body.LivingID;
user.LocationID = req.body.LocationID;
await user.save();
res.json({
IsSuccess: true,
message: 'User updated',
User: user
});
}
else {
res.json({
IsSuccess: true,
message: 'User Not updated',
User: ""
});
}
}
Upvotes: 0
Reputation: 13952
Nope, not possible without another query. This isn't a limitation of Sequelize, it's a limitation of SQL. Under the hood, sequelize needs to do:
update users set role = 'user' where email = '[email protected]'
which will just update the rows, but will not retrieve any rows. Then, it needs to do a second:
select * from users where email = '[email protected]'
to actually get the (now updated) rows.
Upvotes: 4