外键约束“foreign_key”中引用列“column”和引用列“Id”不兼容

问题描述 投票:0回答:4

我正在尝试使用实体框架生成迁移,但我不断遇到

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

我不知道为什么。

我的配置似乎是正确的:

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);
        }
    }

但是当我申请迁移时:

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.

迁移:

    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");

我不明白我做错了什么。

mysql entity-framework entity-framework-core
4个回答
13
投票

我已经找到解决办法了。

我的表带有排序规则:

utf8mb4_unicode_ci
,而我的数据库架构带有
utf8mb4_0900_ai_ci

我将默认服务器架构更改为

utf8mb4_unicode_ci
,它成功了

由于它们是字符,因此它们因模式而不同


8
投票

上述答案并没有为我解决问题。
在我的例子中,

referencing
列数据类型是
int
,这与外键约束中的
referenced
列不同。通过使数据类型相同为我解决了这个问题。
    


0
投票
就我而言,它是不同类型的属性。

引用的属性是:

bigint

虽然引用属性是:

storeId: { type: DataTypes.INTEGER({ unsigned: true }) }

    


0
投票
storeId: { type: DataTypes.INTEGER }

选项来解决问题:

unsigned

  @PrimaryGeneratedColumn({ unsigned: true })
  id!: number;
© www.soinside.com 2019 - 2024. All rights reserved.