Skip to content

A constraint renamed in place frees its name after EF Core's operations: an index created under that name in the same migration fails with 42P07 #99

Closed

Description

@CaffeinatedCoder

Found by the pre-preview sweep of main @ 948496a (lenses: base operations, new check against the standing rules, and an independent read by GPT); reproduced on PostgreSQL 16 and 18.

What happens

An exclusion constraint is renamed, and the same migration has EF Core create an index under the name it gives up. The migration scaffolds and fails at apply:

CREATE INDEX ex_role_grant_active_period ON role_grants ("RoleId");
ALTER TABLE role_grants RENAME CONSTRAINT ex_role_grant_active_period TO ex_renamed;
-- 42P07: relation "ex_role_grant_active_period" already exists

The same with a default name that changes because a column is renamed, while an index takes the old default name.

Why

A contributor's in-place statements are written after EF Core's operations (SchemaOperationPlanner.cs:156, :208; PgExclusionContributor.cs:142). Drops run before them and creates after, so those two are safe; a rename frees its old name too late. NameClaims.Require (:95) reads the target only, where each name is taken once. The other direction has the same shape and was not run: a constraint renamed to a name an EF Core index gives up in that migration is safe only because the rename comes last.

Scenario

public sealed class RangeNamedOld : ITestModel
{
    public static void Build(ModelBuilder modelBuilder)
        => modelBuilder.Entity<RoleGrant>().ToTable("role_grants")
            .DeclarePostgres(pg => [pg.Exclusion(pg.Overlaps(x => x.Period), name: "ex_range")]);
}

public sealed class RangeRenamedAndItsOldNameReused : ITestModel
{
    public static void Build(ModelBuilder modelBuilder)
    {
        var grants = modelBuilder.Entity<RoleGrant>().ToTable("role_grants");
        grants.HasIndex(x => x.RoleId).HasDatabaseName("ex_range");
        grants.DeclarePostgres(pg => [pg.Exclusion(pg.Overlaps(x => x.Period), name: "ex_range_v2")]);
    }
}

[TestMethod]
public void A_name_freed_by_a_rename_can_be_taken_in_the_same_migration()
{
    using var database = new LiveDatabase();
    database.Create<RangeNamedOld>();
    database.Migrate<RangeNamedOld, RangeRenamedAndItsOldNameReused>();   // 42P07
    CollectionAssert.Contains(database.Exclusions(), "ex_range_v2");
}

Change

A direction, to confirm: a rename in place becomes a rebuild when the name it frees is claimed by anything else in the target, or the name it takes was held by anything else in the source. The drop then frees the name before EF Core's operations and the create takes the new one after them. No temporary names.

Test

The pair above in PgExclusionLiveTests, the default-name variant, and the statement order in PgExclusionMigrationTests.

Activity

  1. added this to the 11.0.0 milestone on Nov 14, 2026
  2. added
    bug
    package/core
    package/postgresql
    severity/medium
    area/migrations
    on Nov 14, 2026
  3. CaffeinatedCoder commented on Nov 14, 2026

    @CaffeinatedCoder
    OwnerAuthor

    Measured on PostgreSQL 18 against main @ 010ff31 while fixing this in #137:

    • Both reproductions fail with 42P07 (an explicit name, and a default name after a column rename). So does EF Core renaming an index into the name a renamed constraint gives up.
    • Same defect among declarations: two exclusion constraints on two tables of one schema that swap names fail with 42P07. Each in-place rename takes a relation name the other still holds. Swaps and chains on one table were sound, because they are paired by name and rebuilt.
    • Mirror case: a constraint renamed into a name that an EF Core index drops or renames away in the same migration applies, and the constraint keeps its oid. EF Core's operation runs first, so the fix leaves it a rename.
  4. added a commit that references this issue on Nov 14, 2026
    4fa3c29
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/migrationsbugpackage/corepackage/postgresqlseverity/medium

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions