MySQL Workbench 报告错误 1822:无法添加外键约束。缺少约束索引

MySQL Workbench reports ERROR 1822: Failed to add the foreign key constraint. Missing index for constraint

我正在根据 EER 模型 sql 脚本创建一个 mysql 数据库,并且在尝试进行转换时(全部在 MySQL Workbench 中),我'得到上面的错误。我的目标是从附件 table(sent_from 和 from_to)的 2 列中引用 adoption_entity table 上的 adoption_entity_id。

我有2个table,其中一个是附件:

-- -----------------------------------------------------
-- Table `afth_db`.`attachment`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `afth_db`.`attachment` ;

CREATE TABLE IF NOT EXISTS `afth_db`.`attachment` (
  `attachment_id` BIGINT NOT NULL,
  `sent_from` INT NULL,
  `sent_to` INT NULL,
  `attachment_description` LONGTEXT NULL,
  `attachment_uploaded` DATETIME NULL,
  `attachment_uploaded_by` VARCHAR(255) NULL,
  `adoption_case_num` BIGINT NOT NULL,
  PRIMARY KEY (`attachment_id`),
  INDEX `fk_attachment_adoption_case1_idx` (`adoption_case_num` ASC) VISIBLE,
  UNIQUE INDEX `attachment_id_UNIQUE` (`attachment_id` ASC) VISIBLE,
  UNIQUE INDEX `adoption_case_num_UNIQUE` (`adoption_case_num` ASC) VISIBLE,
  INDEX `fk_attachment_adoption_entity1_idx` (`sent_from` ASC, `sent_to` ASC) VISIBLE,
  UNIQUE INDEX `sent_to_UNIQUE` (`sent_to` ASC) VISIBLE,
  UNIQUE INDEX `sent_from_UNIQUE` (`sent_from` ASC) VISIBLE,
  CONSTRAINT `fk_attachment_adoption_case1`
    FOREIGN KEY (`adoption_case_num`)
    REFERENCES `afth_db`.`adoption_case` (`case_num`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_attachment_adoption_entity1`
    FOREIGN KEY (`sent_from` , `sent_to`)
    REFERENCES `afth_db`.`adoption_entity` (`adoption_entity_id` , `adoption_entity_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB

另一个是收养单位:

-- -----------------------------------------------------
-- Table `afth_db`.`adoption_entity`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `afth_db`.`adoption_entity` (
  `adoption_entity_id` INT NOT NULL,
  `adoption_entity_type` VARCHAR(255) NULL,
  PRIMARY KEY (`adoption_entity_id`),
  UNIQUE INDEX `adoption_entity_id_UNIQUE` (`adoption_entity_id` ASC) VISIBLE)
ENGINE = InnoDB

错误详细说明:

Operation failed: There was an error while applying the SQL script to the database.
ERROR 1822: Failed to add the foreign key constraint. Missing index for constraint 'fk_attachment_adoption_entity1' in the referenced table 'adoption_entity'

我不确定为什么它一直给我这个问题。我已经尝试了几种解决方案,从将 fk_attachment_adoption_entity1 的类型设置为 'unique index'、'index' 以及涉及的其他列,但我似乎无法摆脱错误。我也试过删除并重新创建 1-1 关系,但这也无济于事。如果我在 EER 模型设计上做错了什么,谁能告诉我?

您正在尝试创建 一个 外键引用,它跨越 sent_fromsent_to 两列到 [=13] 中的单行=] table。那不是你想要的。您想要为单独的列 sent_fromsent_to 创建 两个 单独的外键引用。所以约束部分应该是这样的:

CONSTRAINT `fk_attachment_adoption_entity_from`
    FOREIGN KEY (`sent_from`)
    REFERENCES `afth_db`.`adoption_entity` (`adoption_entity_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION),
CONSTRAINT `fk_attachment_adoption_entity_to`
    FOREIGN KEY (`sent_to`)
    REFERENCES `afth_db`.`adoption_entity` (`adoption_entity_id`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)