hiltsyn.blogg.se

Elixir ecto cast
Elixir ecto cast






A later article will go more into why this is a good idea.

#Elixir ecto cast how to

This text has covered how to use UTC DateTimes for Ecto timestamps instead of NaiveDateTime. Make sure to set usec to true or false in depending on the type in the database. in Ecto 2 is the equivalent of in Ecto 3.Īs with Ecto 3 you can put a everywhere use Ecto.Schema is Microseconds have their own separate setting for timestamps: usec which is a boolean. In Ecto 2 (starting from version 2.1) there are two datetime types instead of four. each ( fn table_name -> alter table ( table_name ) do modify :inserted_at, :utc_datetime_usec modify :updated_at, :utc_datetime_usec end end ) end end Ecto 2 Migration def change do # For each of the listed tables, change the type of :inserted_at and :updated_at to microsecond precision ~w/users products another_table/ |> Enum. To make sure the migrations have precision you want (usec or whole seconds), you can specify the type in the migrations when creating a table like so:ĭefmodule YourAppNameHere. For the type in the postgres database, the thing that counts is whether there is a (0) at the end or not: timestamp without time zone(0) for whole seconds and timestamp without time zone for the Ecto types that end in _usec. Ecto 3 migration typeĪs an aside - you might wonder why Ecto uses a Postgres type called “timestamp without time zone” even though we know that the time zone is UTC, but that is a subject for another blog post.

elixir ecto cast

The “(0)” part means that fractional seconds are not stored. In Postgres the type is either timestamp without time zone or timestamp(0) without time zone. It is “_usec” that matters in migrations. If you do not want to use microsecond precision, use :utc_datetime instead of :utc_datetime_usec in your schemas and make sure that the migration for the timestamps match in terms of having “_usec” at the end or not.Ī peculiar detail is that in migrations, unlike schemas, “utc_datetime” and “naive_datetime” both do the same thing. This can be done by using utc_datetime_usec in the migration. In order to have microsecond precision, make sure that the type created in the database table stores microseconds. I like to use :utc_datetime_usec for the timestamps because it has microsecond precision instead of just whole seconds. That being said here is some information about microsecond precision:

elixir ecto cast

If your Ecto project is currrently using naive_datetime for timestamps and you switch to utc_datetime in your schemas, you don’t have to do any changes to migrations for it to work. They are equivalent to the following Elixir types: Ecto 3 type Ecto 3 types for timestamps and microsecondsĮcto 3 has a choice of four types to use for the timestamps function: :utc_datetime, :utc_datetime_usec, :naive_datetime, :naive_datetime_usec I personally tend to prefer using the module attribute. Schema "users" do field :name, :string timestamps () endĮither way works.






Elixir ecto cast