DateTime.truncate(datetime, :second) not working?

This is driving me nut.

I’m simply trying to write the following query:

    Meeting
    |> where([m], m.updated_at < ^DateTime.truncate(update_started_at, :second))

but I still get:

** (ArgumentError) :utc_datetime expects microseconds to be empty, got: #DateTime<2021-02-22 16:50:09.472646Z>

Use `DateTime.truncate(utc_datetime, :second)` (available in Elixir v1.6+) to remove microseconds.

I also tried to apply the truncate function to to m.updated_at part of it but I also get an error.

I’m guessing I could make it work using utc_datetime_usec but I don’t want to write a migration for such basic query.

There must be a way.

Btw: the documentation is pretty unclear on whether the truncate part should apply to the query side or to the interpolated side of the macro.

Thanks for the help

This sounds like you have stored items in the db with microsecond precision and then moved to a newer version of ecto, which now no longer expects those microseconds for a field of type :utc_datetime.

Which one of the two matches: …, got: #DateTime<2021-02-22 16:50:09.472646Z>?

Thanks @LostKobrakai for your help.

This sounds like you have stored items in the db with microsecond precision and then moved to a newer version of ecto

It could have been that but this module is actually quite new so I don’t think this is the case.

Which one of the two matches: …, got: #DateTime<2021-02-22 16:50:09.472646Z>?

It seems like it is the update_started_at datetime, which makes it even less coherent. :frowning:

Can you move the DateTime.truncate outside the query? Because then ecto shouldn’t even know about the non truncated date.

Ok my bad, the issue happened later at:

    meeting
    |> change(%{deleted_at: DateTime.utc_now()})

which works with:

    meeting
    |> change(%{deleted_at: DateTime.truncate(DateTime.utc_now(), :second)})

Sorry for the mistake and thanks for the help!