Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrations scaffolding error when renaming column and adding a table with FK to that column #6258

Closed
mafshin opened this issue Aug 6, 2016 · 0 comments
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@mafshin
Copy link

mafshin commented Aug 6, 2016

Steps to reproduce

  • Generate a migration for the following model and create the database for SQL server via Update-Database command
public class Post
    {
        public int ID { get; set; }
        public string Title { get; set; }
        public string Content { get; set; }
    }

    public class Tag
    {
        public string TagId { get; set; }
    }
  • Change the PostId column to ID
  • Now try to implement Many-To-Many as described here.
  • Now generate the migration

            migrationBuilder.DropPrimaryKey(
                name: "PK_Posts",
                table: "Posts");

            migrationBuilder.DropColumn(
                name: "PostId",
                table: "Posts");

            migrationBuilder.CreateTable(
                name: "PostTag",
                columns: table => new
                {
                    PostId = table.Column<int>(nullable: false),
                    TagId = table.Column<string>(nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_PostTag", x => new { x.PostId, x.TagId });
                    table.ForeignKey(
                        name: "FK_PostTag_Posts_PostId",
                        column: x => x.PostId,
                        principalTable: "Posts",
                        principalColumn: "ID",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_PostTag_Tags_TagId",
                        column: x => x.TagId,
                        principalTable: "Tags",
                        principalColumn: "TagId",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.AddColumn<int>(
                name: "ID",
                table: "Posts",
                nullable: false,
                defaultValue: 0)
                .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

            migrationBuilder.AddPrimaryKey(
                name: "PK_Posts",
                table: "Posts",
                column: "ID");

            migrationBuilder.CreateIndex(
                name: "IX_PostTag_PostId",
                table: "PostTag",
                column: "PostId");

            migrationBuilder.CreateIndex(
                name: "IX_PostTag_TagId",
                table: "PostTag",
                column: "TagId");

The issue

  • Run Update-Database and you will get the following error
Foreign key 'FK_PostTag_Posts_PostId' references invalid column 'ID' in referenced table 'Posts'.
Could not create constraint or index. See previous errors.

If you move the AddColumn and AddPrimaryKey calls before creating the join table, the error will move away.

You may get another error for issue #5660 which can be resolved by removing defaultValue optional parameter.

Further technical details

EF Core version: 1.0
Operating system: Windows 10 x64
Visual Studio version: 2015 Update 3

@divega divega added the type-bug label Aug 8, 2016
@divega divega added this to the 1.1.0 milestone Aug 8, 2016
@bricelam bricelam changed the title Wrong order of migration commands Migrations scaffolding error when renaming column and adding a table with FK to that column Aug 19, 2016
@bricelam bricelam added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 19, 2016
@ajcvickers ajcvickers modified the milestones: 1.1.0-preview1, 1.1.0 Oct 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

4 participants