Search code examples
elixirecto

How to insert in seeds.ex data for Ecto timestamp inserted_at


I'm trying to hardcode some test/initial values into seeds.ex for the Ecto timestamp inserted_at but I'm getting errors:

MyProj.Repo.insert!(%MyProj.MyModel{inserted_at[:month]: 5, name: "nananana", email: "[email protected]", tel: "99999999"})

I've already tried using . instead of brackets but it's the same.


Solution

  • Ecto expects an Ecto.DateTime struct there.

    There are many possibilities to produce this struct out of what you have. As you have a NaiveDateTime, this should work:

    inserted_at: ~N[2017-04-13 00:30:10]
                 |> NaiveDateTime.to_erl()
                 |> Ecto.DateTime.from_erl()
    

    Whether you had an erlang date, it could be even simpler:

    inserted_at: Ecto.DateTime.from_erl({{2017, 4, 13}, {0, 30, 10}})