Repository navigation
Description
Found by a sweep of release/5.x @ d28c938 for base operations the differs do not account for (the shape of #182); reproduced in a scratch PostgresIntegrationTests case on PostgreSQL 18 (Testcontainers).
Problem
Changing the type of a temporal foreign key's key column on both tables in one migration, for example int to long for a room id, fails at apply with 42804. Whether it fails depends on the order EF Core gives the two ALTER COLUMNs, which follows the table names.
EF Core emits one AlterColumnOperation per table. The package emits nothing, because TemporalDescriptor and TemporalForeignKeyDescriptor hold column names only and both are unchanged. No base operation is checked against the columns a temporal foreign key reads. EF Core's own foreign keys survive the same change, because a plain foreign key uses B-tree equality, and integer_ops has cross-type int4/int8 operators. A temporal foreign key uses GiST through btree_gist, which has none. So when the dependent's column is altered first, PostgreSQL rebuilds the key as bigint against an int principal and refuses it.
Model: sw4_a_rooms with HasTemporalConstraint(x => x.RoomId, x => x.ValidDuring), and sw4_z_bookings with HasTemporalForeignKey<SwBooking, SwRoom>(x => x.RoomId, x => x.BookedDuring, x => x.RoomId, x => x.ValidDuring), both on NpgsqlRange<DateOnly> periods. RoomId becomes long on both:
ALTER TABLE sw4_z_bookings ALTER COLUMN "RoomId" TYPE bigint;
ALTER TABLE sw4_a_rooms ALTER COLUMN "RoomId" TYPE bigint;
-- 42804: foreign key constraint "FK_sw4_z_bookings_sw4_a_rooms_RoomId_BookedDuring" cannot be implementedWith the table names the other way round (sw2_z_rooms, sw2_a_bookings), the principal is altered first and the migration applies, since int4 → int8 coerces implicitly. Traced, not run: any key type change with no implicit cast from the dependent's new type to the principal's old one fails the same way (smallint → int, int → uuid).
Change
Give temporal foreign keys an in-the-way rule, the PostgreSQL counterpart of RequiresIndexRecreation. When a base AlterColumnOperation changes the column type (ColumnType or ClrType against OldColumn) of a column the key reads on either side, drop the key before the base operations and add it again after. #184 (generated column change) needs the same mechanism for complex indexes and every constraint kind. One rule may serve both.
The migration fails at apply today, so no database holds its result. The fix changes what this model scaffolds, and it ships on release/5.x.
Tests
- A
PostgresIntegrationTestscase for the model above, with a row in each table. The migration applies, the key exists afterwards, and it still rejects an orphan booking (23503). - The same with the tables named the other way round, which passes today and must keep passing.
- A
NpgsqlDifftest: a type change on a column the key does not read leaves the key alone.
Activity
Merge pull request #193 from CaffeinatedCoder/fix/187-188-temporal-fk
CaffeinatedCoder commentedon Oct 23, 2026
Fixed on release/5.x by #193, shipping in 5.6.0. Closed by hand:
Closesacts only on merges intomain.A type change on a temporal foreign key's key column, on either side, now drops the key before the base operations and adds it after, so the order in which EF Core alters the two tables no longer matters (was 42804).
docs: resolve the CLAUDE.md merge conflict, and repair the line it ca…