Skip to content

Entity splitting not working correctly due to PK constraint naming conflict #3639

Closed

Description

@erick-thompson

This is using the feature where some properties of an entity are mapped to a different table.

The core of the problem is that PK constraint names need to be unique in Postgres. The way that EF generates the PK constraint names results in conflicts when creating the database using EnsureCreated. There appears to be no way to mitigate this due to the way that names are generated.

The EnsureCreated path ends up calling RelationalKeyExtensions.GetName to get the name of the PK constraint. Because both the primary and split mapping have the same entity, and thus the same PrimaryKey, the name of the PK ends up identical on both tables, and the available override via annotation applies to the key, not the StoredObject. The net effect is that EF always creates the same name for both PK constraints, which fails as it's not unique.

The pgcontext.Database.Migrate path does not have the same issue, and there are multiple ways to customize the names.

The lack of a workaround is that there are no injection points to modify the name of the constraint at creation time, and the code doesn't allow for a table/StoredObject scoped constraint name.

I can provide sample code if that would help.

Activity

  1. added theissue type on Oct 17, 2025
  2. added this to the milestone on Oct 17, 2025
  3. roji commented on Oct 17, 2025

    @roji
    Member

    Thanks, this is indeed a bug in the provider - EF does support enforcing uniqueness across things like primary keys, but the PG provider is incorrectly configured. I'll go ahead and fix this.

    Uniqueness tests on primary key, foreign key and index names
    -- PK names must be unique:
    DROP TABLE IF EXISTS a;
    CREATE TABLE a (id integer);
    ALTER TABLE a ADD CONSTRAINT some_pk PRIMARY KEY (id);
    
    DROP TABLE IF EXISTS b;
    CREATE TABLE b (id integer);
    ALTER TABLE b ADD CONSTRAINT some_pk PRIMARY KEY (id);
    -- ERROR: relation "some_pk" already exists
    
    
    -- Index names must be unique:
    DROP TABLE IF EXISTS a;
    CREATE TABLE a (id integer);
    CREATE INDEX some_index ON a (id);
    
    DROP TABLE IF EXISTS b;
    CREATE TABLE b (id integer);
    CREATE INDEX some_index ON b (id);
    -- ERROR: relation "some_index" already exists
    
    -- FK names don't need to be unique
    DROP TABLE IF EXISTS foo;
    CREATE TABLE foo (id integer PRIMARY KEY);
    
    DROP TABLE IF EXISTS a;
    CREATE TABLE a (id integer, foo_id integer);
    ALTER TABLE a ADD CONSTRAINT some_fk FOREIGN KEY (foo_id) REFERENCES foo (id);
    
    DROP TABLE IF EXISTS b;
    CREATE TABLE b (id integer, foo_id integer);
    ALTER TABLE b ADD CONSTRAINT some_fk FOREIGN KEY (foo_id) REFERENCES foo (id);
  4. self-assigned this
    on Oct 17, 2025
  5. added a commit that references this issue on Oct 17, 2025
    dbbfa3b
  6. added 3 commits that reference this issue on Oct 18, 2025
    536141b
    28dfd7c
    45e4cb1
  7. erick-thompson commented on Oct 20, 2025

    @erick-thompson
    Author

    Excellent, thanks!

  8. alienwareone commented on Dec 15, 2025

    @alienwareone

    @roji One of my applications uses multiple schemas with tables that share the same primary key name, which PostgreSQL allows.

    After upgrading to EF Core 10, migrations obviously try to drop and recreate these PKs, breaking foreign key constraints.

    Instead of enforcing uniqueness across all tables, it would be great if SharedTableConvention also considered the schema.

    DROP SCHEMA IF EXISTS a CASCADE;
    DROP SCHEMA IF EXISTS b CASCADE;
    
    CREATE SCHEMA a;
    CREATE SCHEMA b;
    
    CREATE TABLE a.a (id integer);
    ALTER TABLE a.a ADD CONSTRAINT some_pk PRIMARY KEY (id);
    
    CREATE TABLE b.b (id integer);
    ALTER TABLE b.b ADD CONSTRAINT some_pk PRIMARY KEY (id);
    

    FYI: Just replaced the NpgsqlSharedTableConvention with a custom implementation.

    public sealed class ApplicationDbContext : DbContext
    {
        protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
        {
            configurationBuilder.Conventions.Replace<NpgsqlSharedTableConvention>(
                x => ActivatorUtilities.CreateInstance<ApplicationSharedTableConvention>(x));
        }
    }
    
    public sealed class ApplicationSharedTableConvention(
        ProviderConventionSetBuilderDependencies dependencies,
        RelationalConventionSetBuilderDependencies relationalDependencies)
        : NpgsqlSharedTableConvention(dependencies, relationalDependencies)
    {
        protected override bool KeysUniqueAcrossTables
            => false;
    
        protected override bool ForeignKeysUniqueAcrossTables
            => false;
    
        protected override bool IndexesUniqueAcrossTables
            => false;
    
        protected override bool CheckConstraintsUniqueAcrossTables
            => false;
    }
    
  9. roji commented on Dec 16, 2025

    @roji
    Member

    I see... So PK constraint names are scoped to their schema - it's OK to have the same constraint name for two tables, just as long as they're not within the schema.

    /cc @AndriySvyryd

  10. ocoster-sermo commented on Jan 14, 2026

    @ocoster-sermo

    I just hit the exact same issue as @alienwareone - a couple of tables with the same name (and therefore generated PK name) but on different schemas.

    Upgrading from EF Core 8 to EF Core 10 blocked migrations (despite none of our code changing) due to "missing migrations".

    When adding a new migration (again, no code changes) generated a migration dropping and adding of primary keys of some of these tables (appended with a 1). Adding another migration immediately after generates yet another migration with more dropping and adding primary keys on the same tables.

  11. roji commented on Jan 19, 2026

    @roji
    Member

    @alienwareone @ocoster-sermo can you please open a new issue for this with a minimal repro?

  12. ocoster-sermo commented on Jan 19, 2026

    @ocoster-sermo

    @alienwareone @ocoster-sermo can you please open a new issue for this with a minimal repro?

    Done!

  13. modified the milestones: , 10.0.4 on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions