bruno_m_campos

bruno_m_campos

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

Showing Posts 1 to 6

kip

kip

ex_cldr Core Team

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.

josevalim

josevalim

Creator of Elixir

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.

bruno_m_campos

bruno_m_campos OP

Thank you for feedback Kip, i used compare, but i was having issues in matching operations on my tests, at the point where i felt, there should be a better way to handle this :wink:

That’s great news José, thank you for the great work put on those projects, and the feedback of course :smile:

bmurithi

bmurithi

This might be related.

I noticed an oddity in the timestamps during insertion versus lookup:

My tests were failing in serialization of the naive_datetime. After I used the value from db lookup, tests ran fine.

> res = Repo.insert!(%Model{})
> res.inserted_at
~N[2018-01-10 13:25:52.122046]
> res_load = Repo.get!(res.id)
~N[2018-01-10 13:25:52.122000]

Not clear how it is possible for the actual insertion time to be later than reported by insert.

Stack: mongodb 3.4 / Ecto 2.1

josevalim

josevalim

Creator of Elixir

It is possible that Mongo only has milliseconds precision and truncated the value.

Ankhers

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.

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews