我看过其他论坛,包括有关此错误的帖子,但在我的情况下似乎找不到原因。数据类型,排序规则,零填充,非null,无符号和二进制类型都匹配,但我仍然无法提出解决方案。我收到的输出如下:
错误:ERROR: Error 1822: Failed to add the foreign key constraint. Missing index for constraint 'fk_projects_has_equipment_equipment1' in the referenced table 'equipment'
我还包括了设备表。任何帮助将不胜感激。
代码:
CREATE TABLE IF NOT EXISTS `Group_13_Schema`.`project_equipment` (
`project_id` INT NOT NULL,
`vendor_id` INT NOT NULL,
`department_id` INT NOT NULL,
`equipment_id` INT NOT NULL,
`equipment_cost` DECIMAL(10,2) NULL,
PRIMARY KEY (`department_id`, `equipment_id`, `project_id`, `vendor_id`),
INDEX `fk_projects_has_equipment_equipment1_idx` (`vendor_id` ASC, `department_id` ASC, `equipment_id` ASC) VISIBLE,
INDEX `fk_projects_has_equipment_projects1_idx` (`project_id` ASC) VISIBLE,
CONSTRAINT `fk_projects_has_equipment_projects1`
FOREIGN KEY (`project_id`)
REFERENCES `Group_13_Schema`.`projects` (`project_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_projects_has_equipment_equipment1`
FOREIGN KEY (`vendor_id` , `department_id` , `equipment_id`)
REFERENCES `Group_13_Schema`.`equipment` (`vendor_id` , `department_id` , `equipment_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
CREATE TABLE IF NOT EXISTS `Group_13_Schema`.`equipment` (
`equipment_id` INT NOT NULL,
`vendor_id` INT NOT NULL,
`department_id` INT NOT NULL,
`equipment_name` VARCHAR(45) NULL,
`equipment_cost` DECIMAL(10,2) NULL,
PRIMARY KEY (`equipment_id`, `vendor_id`, `department_id`),
INDEX `fk_departments_has_vendors_vendors1_idx` (`vendor_id` ASC) VISIBLE,
INDEX `fk_departments_has_vendors_departments1_idx` (`department_id` ASC) VISIBLE,
CONSTRAINT `fk_departments_has_vendors_departments1`
FOREIGN KEY (`department_id`)
REFERENCES `Group_13_Schema`.`departments` (`department_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_departments_has_vendors_vendors1`
FOREIGN KEY (`vendor_id`)
REFERENCES `Group_13_Schema`.`vendors` (`vendor_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_projects_has_equipment_equipment1`
FOREIGN KEY (`equipment_id`)
REFERENCES `Group_13_Schema`.`equipment` (`vendor_id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
答案 0 :(得分:0)
尝试为3个FK列创建索引。我希望PK已经合格,但是最后2列的顺序与FK约束中列出的顺序相反。