Can we update a record without Ecto changing the "updated_at" field?

I couldn’t find an option in the Repo.update function that would skip setting updated_at. Is there a way?

Use Repo.update_all it will not update autogenerated fields like the updated_at columns.

https://hexdocs.pm/ecto/Ecto.Repo.html#c:update_all/3

2 Likes

Maybe using Ecto.Changeset.force_change and using the current timestamp does work.

2 Likes

Repo.update_all does not work on a single record that’s already loaded. And I am streaming them via Repo.stream and my callback function only receives them one by one.

Repo.update_all would be good for mass operations where there’s no logic attached but that’s not my case – I need to modify data in each record and that depends on what information it has inside it.

I don’t like this and I’ve spent some time looking for other solutions but couldn’t find anything reasonable. So I ended up doing what you suggested.

You could also try if you can remove the change in prepare_changes. Maybe the updated timestamps are already added there.

I glanced at that before but since my goal is for the code to be short and simple, consciously decided not to dig in there.

Still curious if you are right. Might check in a future point.