包含属性的更新 updated_at

Upsert with attribute included updated_at

我想用 gorm 作为 ORM,将一些脚本数据从旧数据库回填到新数据库,我想使用 FirstOrCreate 进行更新,下面是属性和查询使用:

user {
  ID: someid,
  Name: somename,
  .
  .
  .
  CreatedAt: time.Time,
  UpdatedAt: 2020-03-24 17:57:00,
}

err = db.Where(user{ID: someid}).Assign(user).FirstOrCreate(&user)

我想要在新数据库中,数据是原样 (updated_at = 2020-03-24 17:57:00) 但我得到 updated_attime.Now()

如何在发送时更新 updated_at 数据?

FirstOrCreate() 在给定 ID 存在数据时调用 Updates()Updates() 操作会执行模型的 BeforeUpdate, AfterUpdate 方法,更新它的 UpdatedAt 时间戳,更新时保存它的 Associations,如果你不想调用它们,你可以当数据不存在时,使用 UpdateColumnUpdateColumns 和单独的 Create 操作。

参考:Gorm official document about update