bruno_m_campos
Custom fields with naive_datetime type report difference in microseconds on tests
Hi everyone,
I added two custom fields with naive_datetime type to one of my schemas and even though everything seems right (inserting, loading data), when i run tests against it, it gives an error comparing values on microseconds (0 != 6).
An example of the difference:
lhs: %NaiveDateTime{calendar: Calendar.ISO, day: 10, hour: 0, minute: 0, month: 11, second: 0, year: 2018, microsecond: {0, 6}}
rhs: %NaiveDateTime{calendar: Calendar.ISO, day: 10, hour: 0, minute: 0, month: 11, second: 0, year: 2018, microsecond: {0, 0}}
I saw someone mentioning to add @timestamps_opts [usec: false] before your schema settings but that only applies to the timestamps fields AFAIK.
Am i missing something simple here ?
Note: I’m using Phoenix 1.3, Ecto 2.2, PostgreSQL as DB and creating simple fixtures functions on the tests.
Thank you
Most Liked
josevalim
In Ecto master we will have naive_datetime and naive_datetime_usec, exactly to solve issues like this. Elixir v1.6 will also include a NaiveDateTime.truncate function, which makes it easier to set a given precision.
kip
IIRC (and I may not) Ecto will set the microsecond precision to 6 because thats what Postgres will return whereas the default NaiveDateTime will set the precision to 0.
If you use NaiveDateTime.compare/2 I think you’ll find they’ll be :eq meaning equal.
Ankhers
Yes, unfortunately there is an issue at the moment with how the mongo ecto adapter handles the precision. It is something that is on my todo list.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









