Skip to content

Primary key name not taken into account when scaffolding #12821

Closed

Description

@roji

Originally raised by @WarrenFerrell in npgsql/efcore.pg#538 (comment).

When scaffolding primary key constraints, the constraint name currently seems to be ignored. In other words, scaffolding the following table:

CREATE TABLE my_table (
	id INTEGER,
	CONSTRAINT table_pk PRIMARY KEY (id)
);

results in a model where the constraint name isn't set. If the database is recreated from this model, the PK constraint is called PK_my_table rather than table_pk, which is great if round-trippability is a goal. I've verified and the DatabasePrimaryKey returned by NpgsqlDatabaseModelFactory indeed contains the correct constraint name.

I'm guessing this is probably the case for other constraint types (foreign key, unique).

Activity

  1. smitpatel commented on Jul 27, 2018

    @smitpatel
    Contributor

    Only case with PK. AK/Index/FK works correctly.

  2. self-assigned this
    on Jul 27, 2018
  3. added
    help wanted
    on Jul 27, 2018
  4. smitpatel commented on Jul 27, 2018

    @smitpatel
    Contributor
  5. roji commented on Jul 27, 2018

    @roji
    MemberAuthor

    Sure, if @bricelam doesn't mind giving this up, although it might take me a bit of time to get around to it...

  6. bricelam commented on Jul 28, 2018

    @bricelam
    Contributor

    It'll take me a while to get around to it too, so just whoever gets to it first...

  7. added 2 commits that reference this issue on Jul 29, 2018
    00cc206
    893f72b
  8. roji commented on Jul 29, 2018

    @roji
    MemberAuthor

    Submitted #12836

  9. added this to the milestone on Jul 30, 2018
  10. added a commit that references this issue on Jul 30, 2018
    7fff462
  11. removed their assignment
    on Jul 31, 2018
  12. added
    good first issue
    on May 31, 2019
  13. modified the milestones: , 2.2.0 on Nov 11, 2019
  14. OskarKlintrot commented on Apr 22, 2020

    @OskarKlintrot

    Sorry for posting on a closed issue but I'm having a similar problem to this. I'm trying to use EF Core on an existing database that has a table with this schema:

    CREATE TABLE [dbo].[Example] (
        [Id]                  INT                IDENTITY (1, 1) NOT NULL,
        PRIMARY KEY CLUSTERED ([Id] ASC)
    );

    However EF Core reverse engineers the table to

    CREATE TABLE [dbo].[Example] (
        [Id]                  INT                IDENTITY (1, 1) NOT NULL,
        CONSTRAINT [PK_Example] PRIMARY KEY CLUSTERED ([Id] ASC)
    );

    Is there any way, using fluent api for example, to get rid of the name and constraint so that it actually matches the schema of the existing database? I'm not sure the constraint name actually matters in real life? It gives me a lot of false diffs though when I use SqlSchemaCompare in VS to make sure the new entities matches the database.

  15. shvmgpt116 commented on Jul 31, 2020

    @shvmgpt116

    @roji @ajcvickers This issue seems to be present in 2.1 code line of EFCore (I verified it with 2.1.11 and 2.1.14 version of EFCore). Since 2.1 is an LTS, is there any plan to backport the fix to 2.1 code line?

  16. ajcvickers commented on Jul 31, 2020

    @ajcvickers
    Contributor

    @shvmgpt116 We have no plans to port this change to 2.1. At this point in 2.1's release cycle it would have to be an extremely serious issue to be ported there.

  17. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

customer-reportedgood first issuehelp wanted

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions