包含属性的Upsert已更新 [英] Upsert with attribute included updated_at
问题描述
我想使用 gorm
作为ORM将一些脚本回填数据从旧数据库复制到新数据库,并且我想使用 FirstOrCreate
进行增补,如下所示使用的属性和查询:
I want to make some script backfill data from an old database to a new database, with gorm
as it ORM, and I want to upsert using FirstOrCreate
, below are the attributes and query used:
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_at
是 time.Now()
I want in new database, the data is as it is (updated_at = 2020-03-24 17:57:00
) but I got updated_at
is time.Now()
如何在发送时使用 updated_at
更新数据?
How can I upsert the data with updated_at
is updated as I send it?
推荐答案
FirstOrCreate()
调用 Updates()
>. Updates()
操作将执行模型的 BeforeUpdate
, AfterUpdate
方法,更新其 UpdatedAt
时间戳,并在发生以下情况时保存其关联更新,如果您不想调用它们,可以在数据不存在时使用 UpdateColumn
, UpdateColumns
和单独的 Create
操作.
FirstOrCreate()
calls Updates()
when data exist with given ID
.
Updates()
operation will perform the model's BeforeUpdate
, AfterUpdate
method, update its UpdatedAt
timestamp, save its Associations when updaing, if you don't want to call them, you could use UpdateColumn
, UpdateColumns
with separate Create
operation when data is not exist.
参考:有关更新的官方官方文件
这篇关于包含属性的Upsert已更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!