I have a Table called Seasons where columns are "start_at" and "end_at". these columsn contain year values like 2019 and 2020 and so on. I want to update one row which has the highest value in "end_at" column. I tried something like this:
Season.update({is_current: 0}, {where: {is_current: 1}}))
.then(() => Season.update({is_current: 1}, {
where: sequelize.where(
sequelize.fn("MAX", sequelize.col("end_at"))
)
}))
But it gives me an error:
Error: Invalid use of group function
Can somebody help me?
You can do it like this :
Season.findOne({
where : { is_current : 0 }, // find records which is_current = 0;
order : [ ['end_at', 'DESC'] ] // order by desc will make highest to the first record
}).then(ref => {
if(ref) { // <-- Check if any record exists
ref.is_current = 1; //<--- Update Value in instance
return ref.save(); // <-- Save to DB
}
});
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments