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.
Maybe using Ecto.Changeset.force_change and using the current timestamp does work.
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.
I like it ![]()
Hmm it appears this does not work anymore, or at least not for us (Postgres, ecto + ecto_sql 3.13.2, :utc_datetime_usec timestamps) it is always updating updated_at. Can someone confirm?
Nope, just tried it and it works as expected.
Thank for trying! Not sure what is updating updated_at in my project, but moved on with a different solution anyway ![]()
The reason to use force_change, if anyone is wondering, is that it will add the change to the changeset even if the “new” value is the same as the existing value. Which it would be, if you were setting updated_at to itself.
The docs for force_change don’t actually explain what it does, strangely.






















