Skip to content

A temporal foreign key whose name changes skips the in-the-way rules: principal rename plus key retype fails (42804) #196

Closed

Description

@CaffeinatedCoder

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

  1. added this to the 5.6.0 milestone on Oct 23, 2026
  2. added
    bug
    severity/medium
    package/postgresql
    area/constraints
    area/migrations
    on Oct 23, 2026
  3. CaffeinatedCoder commented on Oct 23, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Reached independently by a second lens of the same sweep (new-check-vs-invariant): an explicit name: change on HasTemporalForeignKey together with an int → long key retype emits AlterColumn ×2 then RENAME CONSTRAINT, and fails on PostgreSQL 18 with 42804 in both orders of the table names. Same root cause: of the four rename loops (index InTheWay(to), exclusion InTheWay(renamed), temporal constraint ConstraintInTheWay(renamed), temporal FK), only the FK loop asks no in-the-way question.

  4. added a commit that references this issue on Oct 23, 2026
  5. CaffeinatedCoder commented on Oct 23, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Fixed on release/5.x by #198, shipping in 5.6.0. Closed by hand: Closes acts only on merges into main.

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/constraintsarea/migrationsbugpackage/postgresqlseverity/medium

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions