Entity Core Error: Constraint “FK_Product_Category_CategoryId” of relation “Product” does not exist

I was using .NET Entity Core for my .NET WebApi project. A table called Product had a one-to-one relationship with a table called Category. I wanted to change it so that instead it was a many-to-many relationship. When I did so I got an error:

constraint "FK_Product_Category_CategoryId" of relation "Product" does not exist

I went to my Postgres database and found that the foreign key constraint on Product to Category was not called “FK_Product_Category_CategoryId” but was instead called “FK_Item_Category_CategoryId”. At one point in the past Product was called Item. I have made incremental Entity migration changes as the project evolved one of the migrations created the foreign key constraints when Product was still called Item.

This was a migration removing one-to-one and creating one-to-many

The migration failed because the one-to-one foreign key constraint had a different name than the one Entity Core was expecting.

.NET Entity Core Expects a Certain Naming Pattern for it's Automation of Migrations

Entity Core expects foreign keys for one-to-one relationships to be of the naming convention for an OBJECT1:
FK_OBJECT1NAME_OBJECT2NAME_OBJECT2KEY

But if OBJECT1 changes its name changed to OBJECT1A then sometimes the constraint is not updated during incremental migrations and in the end if you need to undo your one-to-one relationship foreign key and its constraint then because Entity expects the constraint to have the new name of the object (Object1a) then you will either have 2 options to solve the problem:

Update your constraint to the name that Entity is expecting from your migration file

https://stackoverflow.com/questions/971786/constraint-name-update-in-postgresql

Or

Update your Entity core migration file so that it looks for the constraint names as they are named in the database.

How I Fixed this Issue in My Project

Here is part of my Entity Core migration file. This has a drop action on a foreign key constraint but that constraint name is called differently in the database (see bolded text).


namespace ShopBack.Migrations
{
    /// <inheritdoc />
    public partial class ChildCategoryAddedToCategory : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Product_Category_CategoryId",
                table: "Product");

            migrationBuilder.DropIndex(
                name: "IX_Product_CategoryId",
                table: "Product");
           ....
    }
}

The below image has the corrected constraint name (text is bolded). It is clear that this happened during one of my various incremental migration changes because of Product being renamed from Item to Product but I am not certain when exactly the problem was introduced.


namespace ShopBack.Migrations
{
    /// <inheritdoc />
    public partial class ChildCategoryAddedToCategory : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_Item_Category_CategoryId",
                table: "Product");

            migrationBuilder.DropIndex(
                name: "IX_Item_CategoryId",
                table: "Product");
           ....
    }
}

Changing the name to the above and then running:

Update-Database -context LibraryContext

fixed my migration issue and my Product Category relation is now many-to-many rather than one-to-one.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *