-
Notifications
You must be signed in to change notification settings - Fork 215
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Error when using DateTime.Date #2348
Comments
This is because of #2333, which was a bugfix in 6.0.4 (the explanation is here). In a nutshell, date_trunc without a time zone (that 3rd argument) takes TimeZone into account when extracting the date, so produces wrong results (.NET DateTime.Date simply returns the Date component without any conversions). Unfortunately that overload of date_trunc was only introduced in PG12. If you make sure that your TimeZone is always UTC, then it should be OK to map date_trunc as a function, until you upgrade to a newer PG version. It's not an ideal situation, but hopefully it's a sufficient workaround - please let me know if you have more questions. |
Thanks! We will use mapped function until we can upgrade. |
Closing as there's nothing actionable remaining on the Npgsql side. |
I'm quite surpised this is closed. Use of someField.Date in LINQ queries will be very commonplace, and people will not expect it to break so fundamentally on a 'revision' package version update. This is the temporary workaround we are implementing on our Dev databases:
|
@mbirtwistle it's indeed unfortunate that a change with this impact needed to get merged in a patch release - this is not something we do lightly. However, it's important to realize that the previous behavior was incorrect, and resulted in wrong results, as an implicit, unwanted timezone conversion could occur. This is the only reason I decided it was acceptable to do this in a patch. Note that your workaround is only safe if your PostgreSQL database TimeZone is configured to UTC. Otherwise you're reverting to the pre-6.0.4 behavior where implicit, unwanted timezone conversions occur.
That seems very overstated - there's exactly one translation that's affected. Everything else works just as before. |
We recently upgraded to EF Core 6 and along with it Npgsql.EntityFrameworkCore.PostgreSQL from version 5.0.10 to 6.0.4 and came across a curious issue. It appears that any query that references a DateTime mapping but also is only interested in the date portion is now returning an error: 42883: function date_trunc(unknown, timestamp with time zone, unknown) does not exist
As part of the upgrade all of our timestamp columns were altered to be timestamp with timezone and all data was already being written to the DB as UTC.
We are currently using postgres:11 (docker container for development).
Details:
When a simple query like
List<UserProfile> profiles = this.dbContext.UserProfile.Where(u => u.CreatedDateTime <= DateTime.UtcNow.AddDays(-10).Date).ToList();
is run It fails with the following:
This query ran fine previously and on an interesting note, if I change the DB to Postgres 12, 13 or 14 the query also executes as expected.
My current work around is to map date_trunc as a function and explicitly call it but it seems odd to have to do that. We are also looking at upgrading to a newer version of Postgres but that is further out.
Please let me know if this should be posted else where or if you need any additional information.
Thanks
The text was updated successfully, but these errors were encountered: