where(gt) with timestamp returns wrong result
Might be a SQL inherent thing, but I'm a bit puzzled as to why this happens:
Edit: for terseness I left out some code. I think the issue is clear
15 Replies
Seeing that I couldn't find anything else in the discord regarding that, I have created an issue: https://github.com/drizzle-team/drizzle-orm/issues/1148
What are you trying to accomplish with
gt
function?
firstThing
is virtually going to be the same as secondThing
Depending on your fsp value they might actually be the same
I believe in in Mysql the default fsp is 0. So your precision will be 1 second.
Give this a try:
Also, check what kind of precision your database is using to store the timestampIn pgAdmin I can see that the stored timestamp of secondThing is
greater than
the one of firstThing.
I would expect this to be my ground truth in regards to sql queries.
If I change the timestamp's mode
to string
it works. I'm assuming there's some precision lost in the conversion to a js Date
?That might be the case. The date in Js is equivalent to a millisecond precision or FSP=3
But that still doesn't explain though, why a
gt
operator returns the same result it is comparing against.
Note that the bug is not only that it doesn't return secondThing
but that it does return firstThing
the very value of which I was gt
ing againstCan you enable logging in Drizzle to see what is that query producing?
If
firstThing
(as a js value) has a lower precision than the actual value in the db. It might actually be lower
Which would explain why you're getting that resultMode: string
Mode: Date
2023-08-30 18:35:58.540306
vs 2023-08-30T18:36:37.151Z
Yeah, there it is.. The problem is the precision in the
Date
Object
I feel like the convenience of using native Date object outweighs the importance of having fsp=6But it totally breaks expected behavior in this case.
It is the default mode after all
How do you think Drizzle should handle it? We can't force
Date
to have higher precisionI am not sure how to properly handle it. At the very least I would expect a Warning box in the docs to say "if you need high precision, don't go for Date"
But it's up to the developer and the requirements of the app
If you created an Issue in Github, would you be able to add what we've found and the suggestion to add a warning?
Sure, I'll do that
Great, glad I could help 👍🏻
Thank you