fix #1340: DateTime fields error in npgsql#1353
Merged
Merged
Conversation
…ross database schema (towards #1340) - Added migration to alter timestamp columns in various tables to 'timestamp without time zone' to ensure proper handling of date and time values. - Updated model snapshot to reflect changes in column types for consistency.
…ecks and `SpecifyKind` for API key expiration (towards #1340)
…reSQL npgsql compatibility
This comment has been minimized.
This comment has been minimized.
…40-timezone-pgsql-fix-2026-05-30-01-40-42 Add License and Copyright Headers
npgsql
aharwood2
approved these changes
Jun 11, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1340. Resolves errors saving
DateTimefields, and a further crash on startup introduced by the original fix when running withDB_PROVIDER=postgresqlcaused by Npgsql 6+'s strictDateTime/ timestamp type enforcement.PPMToolContext.csAdded code to
OnModelCreatingthat maps allDateTimeandDateTime?properties totimestamp without time zoneonly when the PostgreSQL provider is active. This tells Npgsql to treat all date/time values as local/unspecified rather than requiring UTC.Without the
OnModelCreatingblock, EF Core defaults to mappingDateTime->timestamptzon PostgreSQL. The DB columns are nowtimestamp without time zoneafter the migration, but EF Core would still sendtimestamptzparameters for every query, and PostgreSQL would reject every comparison. The error raised was:Migration
20260527120344_FixDateTimeTimezoneGenerated the migration for Npgsql, which converts 20 existing
timestamptzcolumns totimestamp without time zone.This didn't work out of the box. I had to re-write the
Up()to use raw SQL with aUSING col AT TIME ZONE 'UTC'clause. The generatedAlterColumnomitted this clause and PostgreSQL rejects the cast without it.Up()was originally generated with:... which EF Core was translating to
ALTER TABLE "Projects" ALTER COLUMN "StartDate" TYPE timestamp without time zone;. But PostgreSQL requires aUSINGclause whenever the cast between the old and new type isn't implicit, whichtimestamptz->timestampis not. PostgreSQL didn't know which timezone to use to strip the offset. WithoutUSING, it threw:I added
USING "StartDate" AT TIME ZONE 'UTC'to fix this, though this doesn't quite match the hint, which as-written would just cast without timezone conversion!InnateCodeService.csReplaced
DateTime.UtcNowwithDateTime.Today.DateTime.UtcNowhasKind=Utc, which Npgsql maps totimestamptz, producing a type mismatch against the updatedtimestampEndDatecolumn.DateTime.Todayis correct here sinceEndDateis just storing a regular date (where timezone precision is meaningless) in a timestamp field.ApiKeyConfigComponent.razorWrapped two
DateTime.UtcNow.AddDays(n)calls withDateTime.SpecifyKind(..., DateTimeKind.Unspecified). Without this, saving an API key crashed because Npgsql rejects storing aKind=Utcvalue into atimestamp without time zonecolumn. See the Npgsql datetime documentation:Program.csChanged
Migrate()toMigrateAsync(). On Npgsql 10, the synchronous overload throws anInvalidCastExceptioninternally which prints non-fatalfail: Program[0]logs, and the migration never runs.See the Npgsql 10.0 release notes and npgsql/npgsql#5865 for the stated direction away from synchronous APIs. Confirmed safe on SQLite, which wraps sync operations in the async API as documented behaviour:
Testing done
DB_PROVIDER=sqlite: migrations apply, app starts, seeding works, no regressions that I can see!DB_PROVIDER=postgresql: migrations apply, Projects and People pages now load without timezone errors, API key creation saves without error.