Referencing column 'column' and referenced column 'Id' in foreign key constraint 'foreign_key' are incompatible

Viewed 6924

I am trying to generate a migration using entity framework but I keep bumping into

Referencing column 'TaskId' and referenced column 'Id' in foreign key constraint 'FK_WorkerTaskTargetReference_WorkerTask_TaskId' are incompatible.

And I have no idea why.

My configuration seems to be correct:

public class WorkerTaskConfiguration : EntityConfiguration<WorkerTask>
    {
        public override void Configure()
        {
            Entity.Property(x => x.CreatedBy).IsRequired().HasMaxLength(DataModelConstants.DEFAULT_FIELD_LENGTH);
            Entity.Property(x => x.UpdatedBy).IsRequired().HasMaxLength(DataModelConstants.DEFAULT_FIELD_LENGTH);
            Entity.HasMany(x => x.Messages).WithOne(x => x.Task).HasForeignKey(x => x.TaskId).IsRequired();
            Entity.HasMany(x => x.TargetReferences).WithOne(x => x.Task).HasForeignKey(x => x.TaskId).IsRequired();
        }
}

  public class WorkerTaskTargetReferenceConfiguration : EntityConfiguration<WorkerTaskTargetReference>
    {
        public override void Configure()
        {
            Entity.HasKey(e => e.Id);
            Entity.Property(e => e.CreatedBy).IsRequired().HasMaxLength(DataModelConstants.DEFAULT_FIELD_LENGTH);
            Entity.Property(e => e.UpdatedBy).IsRequired().HasMaxLength(DataModelConstants.DEFAULT_FIELD_LENGTH);
        }
    }

But when I am applying my migration:

Failed executing DbCommand (3ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
CREATE TABLE `WorkerTaskTargetReference` (
    `Id` char(36) NOT NULL,
    `Type` int NOT NULL,
    `ReferencedId` char(36) NOT NULL,
    `CreatedOn` datetime(6) NOT NULL,
    `UpdatedOn` datetime(6) NOT NULL,
    `CreatedBy` varchar(512) CHARACTER SET utf8mb4 NOT NULL,
    `UpdatedBy` varchar(512) CHARACTER SET utf8mb4 NOT NULL,
    `TaskId` char(36) NOT NULL,
    CONSTRAINT `PK_WorkerTaskTargetReference` PRIMARY KEY (`Id`),
    CONSTRAINT `FK_WorkerTaskTargetReference_WorkerTask_TaskId` FOREIGN KEY (`TaskId`) REFERENCES `WorkerTask` (`Id`) ON DELETE CASCADE
);
MySql.Data.MySqlClient.MySqlException (0x80004005): Referencing column 'TaskId' and referenced column 'Id' in foreign key constraint 'FK_WorkerTaskTargetReference_WorkerTask_TaskId' are incompatible.

Migration:

    migrationBuilder.CreateTable(
        name: "WorkerTaskTargetReference",
        columns: table => new
    {
        Id = table.Column<Guid>(nullable: false),
        Type = table.Column<int>(nullable: false),
        ReferencedId = table.Column<Guid>(nullable: false),
        CreatedOn = table.Column<DateTimeOffset>(nullable: false),
        UpdatedOn = table.Column<DateTimeOffset>(nullable: false),
        CreatedBy = table.Column<string>(maxLength: 512, nullable: false),
        UpdatedBy = table.Column<string>(maxLength: 512, nullable: false),
        TaskId = table.Column<Guid>(nullable: false)
    },
        constraints: table =>
        {
            table.PrimaryKey("PK_WorkerTaskTargetReference", x => x.Id);
            table.ForeignKey(
                name: "FK_WorkerTaskTargetReference_WorkerTask_TaskId",
                column: x => x.TaskId,
                principalTable: "WorkerTask",
                principalColumn: "Id",
                onDelete: ReferentialAction.Cascade);
        });

    migrationBuilder.CreateIndex(
        name: "IX_WorkerTaskTargetReference_TaskId",
        table: "WorkerTaskTargetReference",
        column: "TaskId");

And I do not understand what I am doing wrong.

2 Answers

I have found out the solution.

My tables where with collation: utf8mb4_unicode_ci, while my database schema was with utf8mb4_0900_ai_ci

I changed my default server schema to utf8mb4_unicode_ci and it did the trick

Since they were chars, they are different because of schema

The above answer did not solve it for me.
In my case the referencing column data type was int, and that differed from the referenced column that was bigint in the foreign key constraint. By making the data types the same solved it for me.

Related