MySQL Workbench报告错误1822:未能添加外键约束。缺少约束的索引
我正在根据EER模型sql脚本创建一个mysql数据库,当尝试进行转换时(全部在mysql工作台中),我得到了上面的错误。我的目标是从附件表中的2列(从和从发送到)引用收养实体表上的收养实体id 我有两个表格,其中一个是附件:MySQL Workbench报告错误1822:未能添加外键约束。缺少约束的索引,mysql,mysql-workbench,Mysql,Mysql Workbench,我正在根据EER模型sql脚本创建一个mysql数据库,当尝试进行转换时(全部在mysql工作台中),我得到了上面的错误。我的目标是从附件表中的2列(从和从发送到)引用收养实体表上的收养实体id 我有两个表格,其中一个是附件: -- ----------------------------------------------------- -- Table `afth_db`.`attachment` -- --------------------------------------------
-- -----------------------------------------------------
-- 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_附件采用_实体1的索引”以及其他相关列,但我似乎无法消除错误。我也尝试过删除和重新创建1-1关系,但也没有帮助。有人能告诉我,我在EER模型设计中是否做错了什么吗?您正在尝试创建一个外键引用,它跨越
实体表中的两列sent\u from
和sent\u to
到单行。那不是你想要的。您希望为单个列sent\u from
和sent\u 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)