TIL: Custom timestamps fields in Elixir

I have already written many times about the ease of extending Ecto and changing the default behavior. It is possible, for example primary key type used in the schema.

However, this is not all! Today I want to present a way to change the default names for fields created by the timestamps() macro.

Own names and types

Sometimes inserted_at and updated_at are not very convenient, especially if an application should use created_at to support backward compatibility (with other systems). It is also possible to eliminate the default use of naive_datetime and use datetime, which can be very useful for many applications.

Use the code to change the default behavior:

1
timestamps(inserted_at: :created_at, updated_at: :updated_at, type: :datetime)

All options can be pre-configured by setting @timestamps_opts in our schema. You can also check the documentation to learn about additional possibilities.

Get new posts and extra comments

You'll receive every new post with extra unpublished comments available only to the subscribers!

I won't send you spam. Unsubscribe at any time.