简体   繁体   English

Sequelize:手动更新 updatedAt

[英]Sequelize: updating updatedAt manually

Below is the code I am using in Hooks to update the updatedAt column for two objects:下面是我在 Hooks 中用于更新两个对象的updatedAt列的代码:

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?虽然我认为日志看起来是正确的,但为什么数据库中的实际对象没有更新为新的updatedAt值? Or is there an easier way to update an objects updatedAt column?或者是否有更简单的方法来更新对象updatedAt列?

以上都不适合我,所以我不得不改用模型方法:

await MyModel.update({ updatedAt }, { where: { id: instance.id }, silent: true });

The following worked for:以下工作适用于:

group.changed('updatedAt', true)

This will mark the updatedAt column as dirty so it will be updated.这会将updatedAt列标记为脏列,因此它将被更新。

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仅使用 updatedAt = new Date 调用更新是不够的,您必须将列标记为已更改

Accodrding to the docs , you can update an instance value by calling instance.set(key, value, [options]) , so, in your case it should be: 根据 docs ,您可以通过调用instance.set(key, value, [options])来更新实例值,因此,在您的情况下,它应该是:

console.log("BEFORE group.updatedAt " + group.updatedAt)
group.set('updatedAt', new Date())
console.log("CHANGED group.updatedAt " + group.updatedAt)
group.save().then((Group) => { /* the other part of your code*/ })

I was able to update the updatedAt property on a model instance with the .changed() method.我能够使用 .changed() 方法更新模型实例上的 updatedAt 属性。 This only works if you set two property instances to changed = true这仅在您将两个属性实例设置为 changed = true 时才有效

group.changed('updatedAt', true)
group.changed('exampleProperty', true)
await group.save({ silent: false })

我对 Alexander Zinchuk 的代码做了一些小改动,它对我有用:

await MyModel.update({ updatedAt: new Date() }, { where: { id: instance.id }})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM