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

时间:2021-05-01 01:34:36

标签: mysql mysql-workbench

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

我有两张表,其中一张是附件:

-- -----------------------------------------------------
-- 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 以及涉及的其他列,但我似乎无法摆脱错误。我也尝试删除并重新创建 1-1 关系,但这也无济于事。谁能告诉我我在 EER 模型设计方面是否做错了什么?

1 个答案:

答案 0 :(得分:1)

您正在尝试创建一个外键引用,该引用跨越 sent_fromsent_to 两列到 adoption_entity 表中的单行。那不是你想要的。您希望为各个列 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)