MYSQL错误:错误1215:无法添加外键约束

时间:2014-02-23 18:54:06

标签: mysql foreign-keys mysql-workbench workbench

我正在尝试将我的ERD转发到工作台6.0中的方案,但我收到了这个错误。我检查了所有列类型,它们是相同的。我在这里查看了类似的问题,但仍无法找到答案。这是错误日志:

xecuting SQL script in server

ERROR: Error 1215: Cannot add foreign key constraint







-- -----------------------------------------------------

-- Table `proj`.`employee`

-- -----------------------------------------------------

CREATE TABLE IF NOT EXISTS `proj`.`employee` (

  `employee_id` INT NOT NULL,

  `position_id` INT NOT NULL,

  PRIMARY KEY (`employee_id`),

  CONSTRAINT `fk_employee_employee_history1`

    FOREIGN KEY (`employee_id`)

    REFERENCES `proj`.`employee_history` (`employee_id`)

    ON DELETE NO ACTION

    ON UPDATE NO ACTION,

  CONSTRAINT `fk_employee_equipment_rentals1`

    FOREIGN KEY (`employee_id`)

    REFERENCES `proj`.`equipment_rentals` (`handling_employee_id`)

    ON DELETE NO ACTION

    ON UPDATE NO ACTION,

  CONSTRAINT `fk_employee_scheduled_trips1`

    FOREIGN KEY (`employee_id`)

    REFERENCES `proj`.`scheduled_trips` (`leader`)

    ON DELETE NO ACTION

    ON UPDATE NO ACTION,

  CONSTRAINT `fk_employee_scheduled_trips2`

    FOREIGN KEY (`employee_id`)

    REFERENCES `proj`.`scheduled_trips` (`assistant`)

    ON DELETE NO ACTION

    ON UPDATE NO ACTION)

ENGINE = InnoDB



SQL script execution finished: statements: 8 succeeded, 1 failed



Fetching back view definitions in final form.

Nothing to fetch

1 个答案:

答案 0 :(得分:0)

所以事实证明我的外键约束都搞砸了。我的关系倒退了。我没有让外键引用主键,而是让主键是引用非唯一列的外键。我扭转了这一点,并且有效。