MySQL Workbench - 正向工程 - 错误1005:无法创建表(错误号:150)

时间:2013-02-21 22:06:36

标签: mysql mysql-workbench mysql-error-1005 forward-engineer

我在MacOSX上使用MySQL Workbench 5.2.45我使用EER Diagram功能设计了一个数据库,然后尝试了正向工程来生成我的数据库以放在我的本地服务器上。我收到错误代码:错误1005:无法创建表(错误号:150)

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';

CREATE SCHEMA IF NOT EXISTS `Finance` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `Finance` ;

-- -----------------------------------------------------
-- Table `Finance`.`DatabaseUser`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `Finance`.`DatabaseUser` (
  `ID` INT(11) NOT NULL AUTO_INCREMENT ,
  `FirstName` VARCHAR(255) NOT NULL ,
  `LastName` VARCHAR(255) NOT NULL ,
  `Tel` VARCHAR(255) NULL ,
  `Email` VARCHAR(255) NOT NULL,
  `Password` VARCHAR(255) NOT NULL ,
  `Admin` CHAR(1) NOT NULL DEFAULT 'N' ,
  `Project1` CHAR(1) NOT NULL DEFAULT 'N' ,
  `Project2` CHAR(1) NOT NULL DEFAULT 'N' ,
  `Project3` CHAR(1) NOT NULL DEFAULT 'N' ,
  PRIMARY KEY (`ID`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `Finance`.`Entities`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `Finance`.`Entities` (
  `ID` INT(11) NOT NULL AUTO_INCREMENT ,
  `CreatorID` INT(11) NOT NULL ,
  `FullName` VARCHAR(255) NOT NULL ,
  `ShortName` VARCHAR(255) NULL ,
  PRIMARY KEY (`ID`) ,
  INDEX `Creator_idx` (`CreatorID` ASC) ,
  CONSTRAINT `CreatorEntities`
    FOREIGN KEY (`CreatorID` )
    REFERENCES `Finance`.`DatabaseUser` (`ID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

-- -----------------------------------------------------
-- Table `Finance`.`Grant`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `Finance`.`Grant` (
  `ID` INT(11) NOT NULL AUTO_INCREMENT ,
  `CreatorID` INT(11) NOT NULL ,
  `DonorID` INT(11) NOT NULL ,
  `RecipientID` INT(11) NOT NULL ,
  `Name` VARCHAR(255) NOT NULL ,
  `Year` YEAR NOT NULL ,
  PRIMARY KEY (`ID`) ,
  INDEX `Creator_idx` (`CreatorID` ASC) ,
  INDEX `index3` (`DonorID` ASC, `RecipientID` ASC) ,
  CONSTRAINT `CreatorGrant`
    FOREIGN KEY (`CreatorID` )
    REFERENCES `Finance`.`DatabaseUser` (`ID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `InstrumentGrant`
    FOREIGN KEY (`DonorID` , `RecipientID` )
    REFERENCES `Finance`.`Entities` (`ID` , `ID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

USE `Finance` ;


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

我一直在尝试帖子(Error Code: 1005. Can't create table '...' (errno: 150))中建议的解决方案,但这似乎不是问题。 1.数据类型相同 我正在引用主键 3.外键名称是唯一的 4.表格都是InnoDB 5.不涉及SET NULL 等

你能帮忙吗?

2 个答案:

答案 0 :(得分:1)

更改Grant表中的外键约束。

参考http://www.sqlfiddle.com/#!2/04945

  CONSTRAINT `InstrumentGrant`
    FOREIGN KEY (`RecipientID`)
    REFERENCES `Entities` (`ID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `InstrumentGrant_2`
    FOREIGN KEY (`DonorID`)
    REFERENCES `Entities` (`ID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION

根据需要命名约束。

答案 1 :(得分:0)

解决:

在导出菜单中,选择“生成单独的CREATE INDEX语句”选项以避免此错误。