ERROR 1005 MySQL Workbench Forward engineering

2019-08-24 05:42发布

问题:

I was attempting to Forward engineer an EER diagram in MySQL Workbench and I keep on getting this error. I'm pretty new to this so any help would be appreciated as well as anything I could use to improve. Enclosed is the error message and the SQL script. Thanks!

  ERROR: Error 1005: Can't create table 'mnn09c.ASSIGN' (errno: 150)
  CREATE TABLE IF NOT EXISTS `mnn09c`.`ASSIGN` (
  `ASN_ID` INT NOT NULL,
  `ASN_START_D` DATETIME NOT NULL,
  `ASN_END_D` DATETIME NOT NULL,
  `EMP_ID` INT NOT NULL,
   PRIMARY KEY (`ASN_ID`),
   CONSTRAINT `fk_ASSIGN_PRODUCT1`
    FOREIGN KEY (`ASN_ID`)
    REFERENCES `mnn09c`.`PRODUCT` (`ASN_ID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
   ENGINE = InnoDB

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

   Fetching back view definitions in final form.

   Nothing to fetch

    Executing SQL script in server

   ERROR: Error 1005: Can't create table 'mnn09c.ASSIGN' (errno: 150)

   CREATE TABLE IF NOT EXISTS `mnn09c`.`ASSIGN` (

  `ASN_ID` INT NOT NULL,

   `ASN_START_D` DATETIME NOT NULL,

   `ASN_END_D` DATETIME NOT NULL,

    `EMP_ID` INT NOT NULL,

    PRIMARY KEY (`ASN_ID`),

    CONSTRAINT `fk_ASSIGN_PRODUCT1`

    FOREIGN KEY (`ASN_ID`)

    REFERENCES `mnn09c`.`PRODUCT` (`ASN_ID`)

    ON DELETE NO ACTION

    ON UPDATE NO ACTION)

    ENGINE = InnoDB



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

   Fetching back view definitions in final form.

   Nothing to fetch
___________________________________________________________

    `SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
    SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
    SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';

    DROP SCHEMA IF EXISTS `mnn09c` ;
    CREATE SCHEMA IF NOT EXISTS `mnn09c` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci 
    ;
    SHOW WARNINGS;
    USE `mnn09c` ;

    -- -----------------------------------------------------
    -- Table `mnn09c`.`PRODUCT`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `mnn09c`.`PRODUCT` ;

    SHOW WARNINGS;
    CREATE TABLE IF NOT EXISTS `mnn09c`.`PRODUCT` (
    `PROD_ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
    `PROD_DURATION` INT NOT NULL,
    `PROD_EVAL` VARCHAR(45) NOT NULL,
    `ASN_ID` INT NOT NULL,
    PRIMARY KEY (`PROD_ID`))
    ENGINE = InnoDB;

    SHOW WARNINGS;

    -- -----------------------------------------------------
    -- Table `mnn09c`.`ASSIGN`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `mnn09c`.`ASSIGN` ;

    SHOW WARNINGS;
    CREATE TABLE IF NOT EXISTS `mnn09c`.`ASSIGN` (
      `ASN_ID` INT NOT NULL,
      `ASN_START_D` DATETIME NOT NULL,
      `ASN_END_D` DATETIME NOT NULL,
      `EMP_ID` INT NOT NULL,
      PRIMARY KEY (`ASN_ID`),
      CONSTRAINT `fk_ASSIGN_PRODUCT1`
        FOREIGN KEY (`ASN_ID`)
        REFERENCES `mnn09c`.`PRODUCT` (`ASN_ID`)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION)
    ENGINE = InnoDB;

    SHOW WARNINGS;

    -- -----------------------------------------------------
    -- Table `mnn09c`.`EMPLOYEE`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `mnn09c`.`EMPLOYEE` ;

    SHOW WARNINGS;
    CREATE TABLE IF NOT EXISTS `mnn09c`.`EMPLOYEE` (
      `EMP_ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
      `EMP_LNAME` VARCHAR(45) NOT NULL,
      `EMP_FNAME` VARCHAR(45) NOT NULL,
      `EMP_ADDR` VARCHAR(45) NOT NULL,
      `DEPT_ID` INT NOT NULL,
      `DIVS_ID` INT NOT NULL,
      PRIMARY KEY (`EMP_ID`),
      INDEX `fk_EMPLOYEE_DEPT_idx` (`DEPT_ID` ASC),
      CONSTRAINT `fk_EMPLOYEE_DEPT`
        FOREIGN KEY (`DEPT_ID`)
        REFERENCES `mnn09c`.`DEPT` (`DEPT_ID`)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION,
      CONSTRAINT `fk_EMPLOYEE_ASSIGN1`
        FOREIGN KEY (`EMP_ID`)
        REFERENCES `mnn09c`.`ASSIGN` (`EMP_ID`)
        ON DELETE NO ACTION
    ON UPDATE NO ACTION)
    ENGINE = InnoDB;

    SHOW WARNINGS;

    -- -----------------------------------------------------
    -- Table `mnn09c`.`DIVISION`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `mnn09c`.`DIVISION` ;

    SHOW WARNINGS;
    CREATE TABLE IF NOT EXISTS `mnn09c`.`DIVISION` (
      `DIVS_ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
      `DIVS_NAME` VARCHAR(45) NOT NULL,
      `DIVS_URL` VARCHAR(45) NOT NULL,
      `DIVS_EMAIL` VARCHAR(45) NOT NULL,
      `DIVS_LOC` VARCHAR(45) NOT NULL,
      `EMP_ID` INT NOT NULL,
      PRIMARY KEY (`DIVS_ID`),
      UNIQUE INDEX `DIVS_ID_UNIQUE` (`DIVS_ID` ASC),
      UNIQUE INDEX `DIVS_URL_UNIQUE` (`DIVS_URL` ASC),
      UNIQUE INDEX `DIVS_EMAIL_UNIQUE` (`DIVS_EMAIL` ASC),
      INDEX `fk_DIVISION_EMPLOYEE1_idx` (`EMP_ID` ASC),
      CONSTRAINT `fk_DIVISION_EMPLOYEE1`
        FOREIGN KEY (`EMP_ID`)
    REFERENCES `mnn09c`.`EMPLOYEE` (`EMP_ID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
    ENGINE = InnoDB;

    SHOW WARNINGS;

    -- -----------------------------------------------------
    -- Table `mnn09c`.`DEPT`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `mnn09c`.`DEPT` ;

    SHOW WARNINGS;
    CREATE TABLE IF NOT EXISTS `mnn09c`.`DEPT` (
      `DEPT_ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
      `DEPT_NAME` VARCHAR(45) NOT NULL,
      `DEPT_LOCATION` VARCHAR(45) NOT NULL,
      `DEPT_EMAIL` VARCHAR(45) NOT NULL,
      `DEPT_URL` VARCHAR(45) NOT NULL,
      `EMP_ID` INT NOT NULL,
      `DIVS_ID` INT NOT NULL,
      PRIMARY KEY (`DEPT_ID`),
      UNIQUE INDEX `DEPT_ID_UNIQUE` (`DEPT_ID` ASC),
      UNIQUE INDEX `DEPT_EMAIL_UNIQUE` (`DEPT_EMAIL` ASC),
      UNIQUE INDEX `DEPT__UNIQUE` (`DEPT_URL` ASC),
      INDEX `fk_DEPT_DIVISION1_idx` (`DIVS_ID` ASC),
      INDEX `fk_DEPT_EMPLOYEE1_idx` (`EMP_ID` ASC),
      CONSTRAINT `fk_DEPT_DIVISION1`
        FOREIGN KEY (`DIVS_ID`)
        REFERENCES `mnn09c`.`DIVISION` (`DIVS_ID`)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION,
      CONSTRAINT `fk_DEPT_EMPLOYEE1`
        FOREIGN KEY (`EMP_ID`)
        REFERENCES `mnn09c`.`EMPLOYEE` (`EMP_ID`)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION)
    ENGINE = InnoDB;

    SHOW WARNINGS;

    -- -----------------------------------------------------
    -- Table `mnn09c`.`MANAGE`
    -- -----------------------------------------------------
    DROP TABLE IF EXISTS `mnn09c`.`MANAGE` ;

    SHOW WARNINGS;
    CREATE TABLE IF NOT EXISTS `mnn09c`.`MANAGE` (
      `MANAGER_ID` INT UNSIGNED NOT NULL AUTO_INCREMENT,
      PRIMARY KEY (`MANAGER_ID`),
      UNIQUE INDEX `MANAGER_ID_UNIQUE` (`MANAGER_ID` ASC))
    ENGINE = InnoDB;

    SHOW WARNINGS;

    SET SQL_MODE=@OLD_SQL_MODE;
    SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
    SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;

回答1:

This is a foreign key error, it means the table or data you are trying to insert is violating some foreign key constraint.

See: http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html

Typically this is caused by using an SQL dump createde ither by a 3rd party program that doesn't know how to dump foreign keys correctly, or, also older versions of mysqldump.

Download a MySQL 5.5 or 5.6 client and mysqldump the database from scratch.

You may also be able to make it work by re-ordering your SQL file to create the PRODUCT table before the ASSIGN table.



回答2:

This problem occurs because more that one foreign key has the same name! In the workbench, after making the selection of the table that has the FK, go to the foreign keys, and change the name of the foreign key (not the colum name, only the FK name). For example: FK colum name: idUser. FK name: idUser. Change the FK name on this table to idUserA. In any other table that also uses idUser as FK, change the name of the FK to idUserB. OK?

I hope you can understand me. =)