在 Sequelize 中更改 createdAt 的值?

Change value of createdAt in Sequelize?

我想将 createdAt 的值设置为过去的某个时间。

但是,我的所有尝试都未能更改数据库中的 createdAt 日期。该值保持不变。

const yesterday = ( d => new Date(d.setDate(d.getDate()-1)) )(new Date);

// failed attempts
hang.createAt = yesterday;
hang.set('createdAt', yesterday);
hang.changed('createdAt', yesterday);

await hang.save();

我也试过了

const yesterday = ( d => new Date(d.setDate(d.getDate()-1)) )(new Date);

// failed attempt
await hang.update({ createdAt: yesterday });

问题证明

2020-10-07T19:24:29.058Z // before
2020-10-07T19:24:29.058Z // after

如何更改实例的 createdAt 值?

试试这个

const yesterday = ( d => new Date(d.setDate(d.getDate()-1)) )(new Date);

hang.changed('createdAt', true);
hang.set('createdAt', yesterday,{raw: true});
await hang.save({
        silent: true,
        fields: ['createdAt']
 });    

来自文档
set :

raw: true

if a custom setter function is defined for the key, that function will be called instead. To bypass the setter, you can pass raw: true in the options object.

save :

  silent: true

If true, the updatedAt timestamp will not be updated.