gpt4 book ai didi

elixir - Repo.update_all 之后 Ecto 中的 Updated_at

转载 作者:行者123 更新时间:2023-12-04 02:00:33 26 4
gpt4 key购买 nike

我需要更新我的 Asset表所以我需要做这样的事情。

from(a in Asset, 
where: a.id == ^asset.id,
update: [set: [asset_name: "a name"] ]
)
|> Repo.update_all([])

这工作正常,但是 updated_at不更新。

从文档:

Keep in mind this update_all will not update autogenerated fields like the updated_at columns.



那么这是否意味着,我需要将时间传递到我的查询中?类似 DateTime.utc_now
谢谢

最佳答案

是的,您需要自动更新时间戳,所以它应该是这样的:

Asset
|> where([a], a.id == ^asset.id)
|> update([set: [asset_name: "a name", updated_at: Timex.now()]])
|> Repo.update_all([])

我用 Timex.now()在这里,但我猜 DateTime.utc_now()会工作得一样好,但我还没有测试过。

如果您经常这样做,我可能会创建一个通用函数,该函数接受查询并向其添加时间戳更新并返回更新的查询。

关于elixir - Repo.update_all 之后 Ecto 中的 Updated_at,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47569735/

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