gpt4 book ai didi

node.js - 在 Sequelize 中更改 createdAt 的值?

转载 作者:行者123 更新时间:2023-12-03 12:18:27 25 4
gpt4 key购买 nike

我想将 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.

关于node.js - 在 Sequelize 中更改 createdAt 的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64250923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com