Repository navigation
Description
Found by a pre-release sweep of release/5.x @ e563ba1 (lens: base-operations, interactions); reproduced on PostgreSQL 18 in a scratch PostgresIntegrationTests case, and again by the caller.
Problem
#188's fix puts a temporal foreign key in the way of a key-column type change, and #187's drops it first when its principal constraint is dropped, changed or rebuilt. Neither reaches a foreign key whose drop and add differ only by name, because the FK rename loop turns that pair into RENAME CONSTRAINT, placed after the base operations, and its only guard is the #143 name-in-use check:
// NpgsqlComplexIndexMigrationsModelDiffer.cs:891-894 @ e563ba1
if (target is not null
&& IsNameInUse(names ??= TargetNames(target), DefaultSchema(target), dropped.Name, renamed.DependentTable, renamed.DependentSchema, indexBacked: false))
continue;The constraint rename loop above it checks ConstraintInTheWay(renamed) || IsNameInUse(…). The FK loop has no ForeignKeyInTheWay, DependsOnRebuiltConstraint or DependsOnChangedTemporalConstraint.
A default FK name contains the principal table's name, so renaming the principal table is enough to make the pair a rename.
Scenario
tk9_a_rooms (temporal constraint on RoomId, ValidDuring) and tk9_b_bookings (temporal FK to it). One migration renames tk9_a_rooms → tk9_c_rooms and retypes RoomId integer → bigint on both tables:
DropPrimaryKeyOperation tk9_a_rooms
RenameTableOperation tk9_a_rooms
AlterColumnOperation tk9_b_bookings
AlterColumnOperation tk9_c_rooms
AddPrimaryKeyOperation tk9_c_rooms
SQL: ALTER TABLE "tk9_c_rooms" RENAME CONSTRAINT "AK_tk9_a_rooms_RoomId_ValidDuring" TO "AK_tk9_c_rooms_RoomId_ValidDuring";
SQL: ALTER TABLE "tk9_b_bookings" RENAME CONSTRAINT "FK_tk9_b_bookings_tk9_a_rooms_RoomId_BookedDuring" TO "FK_tk9_b_bookings_tk9_c_rooms_RoomId_BookedDuring";
-- 42804: foreign key constraint "FK_tk9_b_bookings_tk9_a_rooms_RoomId_BookedDuring" cannot be implemented
An explicit name: change on HasTemporalForeignKey with the same retype fails the same way. Traced, not run: a principal constraint rebuilt by a recreated period column (#184) or the #143 fallback, together with an FK rename, drops the constraint before the base operations while the FK still references it (2BP01).
Change
Mirror the constraint loop: an FK pair stays a drop before and an add after when ForeignKeyInTheWay(renamed), DependsOnRebuiltConstraint(renamed), DependsOnChangedTemporalConstraint(…) or IsNameInUse(…) holds.
Test
A PostgresIntegrationTests case for the scenario above, with a row in each table: the migration applies, the renamed FK exists, and it rejects an orphan booking (23503). A NpgsqlDiff test for the rebuilt-principal shape: the FK is dropped before the constraint.
Activity
CaffeinatedCoder commentedon Oct 23, 2026
Reached independently by a second lens of the same sweep (new-check-vs-invariant): an explicit
name:change onHasTemporalForeignKeytogether with anint→longkey retype emitsAlterColumn×2 thenRENAME CONSTRAINT, and fails on PostgreSQL 18 with 42804 in both orders of the table names. Same root cause: of the four rename loops (indexInTheWay(to), exclusionInTheWay(renamed), temporal constraintConstraintInTheWay(renamed), temporal FK), only the FK loop asks no in-the-way question.Merge pull request #198 from CaffeinatedCoder/fix/196-temporal-fk-ren…
CaffeinatedCoder commentedon Oct 23, 2026
Fixed on release/5.x by #198, shipping in 5.6.0. Closed by hand:
Closesacts only on merges intomain.A temporal foreign key rename pair now stays a drop before and an add after the base operations when the key is in the way (a retyped or recreated key column), its principal constraint is rebuilt or changed, or its old name is taken, as the index, exclusion and temporal-constraint rename paths already did.