Repo.update_all之后的Ecto中的Updated_at

时间:2017-11-30 09:16:36

标签: elixir ecto

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

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

这可行,但updated_at未更新。

来自文档:

  

请记住,此update_all不会更新自动生成的字段,例如   updated_at列。

这是否意味着,我需要将时间传递给我的查询?像DateTime.utc_now

这样的东西

由于

1 个答案:

答案 0 :(得分:3)

是的,您需要自动更新时间戳,因此它应该是:

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()可以正常工作,但我还没有测试过。

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