Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize: updating updatedAt manually

Below is the code I am using in Hooks to update the updatedAt column for two objects:

hooks: {
                afterUpdate: (group, options, callback) => {
                    console.log("groudId " + groupId + " options " + options)
                },
                afterCreate: (member, options, callback) => {
                    return new Promise((resolve, reject) => {
                        sequelize.models.Group.findOne({
                            where: {
                                id: member.group_id
                            }
                        }).then((group) => {
                            if (group) {
                                var date = new Date();
                                console.log("BEFORE group.updatedAt " + group.updatedAt)
                                group.dataValues.updatedAt = new Date()
                                console.log("CHANGED group.updatedAt " + group.updatedAt)
                                group.save().then((Group) => {
                                    if (Group) {
                                        console.log("UPDATED Group.updatedAt " + Group.updatedAt)
                                        console.log("UPDATED group.updatedAt " + group.updatedAt)
                                        resolve(Group)
                                    } else {
                                        console.log("NO GROUP Found")
                                        return reject(group.id)
                                    }
                                }).catch((error) => {
                                    return (error)
                                })
                            } else {
                                return reject(id)
                            }
                        }).catch((error) => {
                            return (reject)
                        })
                    })
                }

Console Log:

BEFORE group.updatedAt Fri Feb 17 2017 17:36:00 GMT-0800 (PST)
CHANGED group.updatedAt Tue Feb 28 2017 14:00:17 GMT-0800 (PST)
UPDATED Group.updatedAt Tue Feb 28 2017 14:00:17 GMT-0800 (PST)
UPDATED group.updatedAt Tue Feb 28 2017 14:00:17 GMT-0800 (PST)
BEFORE group.updatedAt Fri Feb 17 2017 17:36:00 GMT-0800 (PST)
CHANGED group.updatedAt Tue Feb 28 2017 14:00:19 GMT-0800 (PST)
UPDATED Group.updatedAt Tue Feb 28 2017 14:00:19 GMT-0800 (PST)
UPDATED group.updatedAt Tue Feb 28 2017 14:00:19 GMT-0800 (PST)

While the log, what I think, appears correct, why isn't the actual object in the DB updated to the new updatedAt value? Or is there an easier way to update an objects updatedAt column?

like image 337
user1107173 Avatar asked Feb 28 '17 22:02

user1107173


People also ask

What is update return Sequelize?

Update function of sequelize returns a number of affected rows (first parameter of result array).

How do you update a record 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.

How do you prevent createdAt and updatedAt in Sequelize?

With timestamps: false , the generated model will omit the createdAt and updatedAt attributes. You can also opt to include only the timestamp attribute you need as shown below: const User = sequelize. define( "User", { firstName: Sequelize.


2 Answers

This worked for me

group.changed('updatedAt', true)

await group.update({
    updatedAt: new Date()
})

Calling just update with updatedAt = new Date is not enough, you must flag column as changed

like image 52
l2ysho Avatar answered Oct 15 '22 00:10

l2ysho


The following worked for:

group.changed('updatedAt', true)

This will mark the updatedAt column as dirty so it will be updated.

like image 20
user1107173 Avatar answered Oct 15 '22 02:10

user1107173