You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Entity splitting not working correctly due to PK constraint naming conflict #3639
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.
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:DROPTABLE IF EXISTS a;
CREATETABLEa (id integer);
ALTERTABLE a ADD CONSTRAINT some_pk PRIMARY KEY (id);
DROPTABLE IF EXISTS b;
CREATETABLEb (id integer);
ALTERTABLE b ADD CONSTRAINT some_pk PRIMARY KEY (id);
-- ERROR: relation "some_pk" already exists-- Index names must be unique:DROPTABLE IF EXISTS a;
CREATETABLEa (id integer);
CREATEINDEXsome_indexON a (id);
DROPTABLE IF EXISTS b;
CREATETABLEb (id integer);
CREATEINDEXsome_indexON b (id);
-- ERROR: relation "some_index" already exists-- FK names don't need to be uniqueDROPTABLE IF EXISTS foo;
CREATETABLEfoo (id integerPRIMARY KEY);
DROPTABLE IF EXISTS a;
CREATETABLEa (id integer, foo_id integer);
ALTERTABLE a ADD CONSTRAINT some_fk FOREIGN KEY (foo_id) REFERENCES foo (id);
DROPTABLE IF EXISTS b;
CREATETABLEb (id integer, foo_id integer);
ALTERTABLE b ADD CONSTRAINT some_fk FOREIGN KEY (foo_id) REFERENCES foo (id);
@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.
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.
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.
Activity
roji commentedon Oct 17, 2025
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
Ensure key name uniqueness
Ensure key name uniqueness (#3641)
Ensure key name uniqueness (#3641)
Ensure key name uniqueness (npgsql#3641)
erick-thompson commentedon Oct 20, 2025
Excellent, thanks!
alienwareone commentedon Dec 15, 2025
@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.
FYI: Just replaced the NpgsqlSharedTableConvention with a custom implementation.
roji commentedon Dec 16, 2025
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
ocoster-sermo commentedon Jan 14, 2026
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.
roji commentedon Jan 19, 2026
@alienwareone @ocoster-sermo can you please open a new issue for this with a minimal repro?
ocoster-sermo commentedon Jan 19, 2026
Done!