我从我在 WorkBench 中创建的模型中转发设计了一个脚本,但该脚本一直失败。它在第二个创建表上失败:
CREATE TABLE IF NOT EXISTS `myDatabase`.`UserProfile` (
`ProfileId` INT NOT NULL AUTO_INCREMENT ,
`FirstName` VARCHAR( 45 ) NULL ,
`LastName` VARCHAR( 45 ) NULL ,
`Gender` CHAR( 1 ) NULL ,
`DOB` DATETIME NULL ,
`HairColor` VARCHAR( 20 ) NULL DEFAULT 'No Answer',
`EyeColor` VARCHAR( 20 ) NULL DEFAULT 'No Answer',
`Height` VARCHAR( 10 ) NULL DEFAULT 'No Answer',
`Weight` VARCHAR( 45 ) NULL DEFAULT 'Average',
`UserId` VARCHAR( 45 ) NOT NULL ,
PRIMARY KEY ( `ProfileId` ) ,
CONSTRAINT `FK_User_Profile` FOREIGN KEY ( `UserId` ) REFERENCES `OurAgreement`.`UserAccount` (
`UserId`
) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE = INNODB;
我相信错误 150 是一个外键问题。与此 FK 关联的表首先在脚本中创建,因此在尝试此约束之前它就存在。这是该表的 DDL:
CREATE TABLE IF NOT EXISTS `mydatabase`.`UserAccount` (
`UserId` INT NOT NULL AUTO_INCREMENT ,
`Login` VARCHAR(20) CHARACTER SET 'utf8' COLLATE 'utf8_unicode_ci' NOT NULL ,
`Password` CHAR(64) NOT NULL ,
`Email` VARCHAR(45) NULL ,
PRIMARY KEY (`UserId`))
ENGINE = InnoDB;
有什么想法吗?
编辑=============================
带有 2 个 FK 的表:
CREATE TABLE IF NOT EXISTS `MyDatabase`.`Answer` (
`AnswerId` INT NOT NULL ,
`QuestionId` INT NOT NULL ,
`ProfileId` INT NOT NULL ,
PRIMARY KEY ( `AnswerId` ) ,
INDEX `ProfileId_idx` ( `ProfileId` ASC ) ,
INDEX `QuestionId_idx` ( `QuestionId` ASC ) ,
CONSTRAINT `FK_Question_Answer` FOREIGN KEY ( `QuestionId` ) REFERENCES `MyDatabase`.`Question` (
`QuestionId`
) ON DELETE NO ACTION ON UPDATE CASCADE ,
CONSTRAINT `FK_Answer_Profile` FOREIGN KEY ( `ProfileId` ) REFERENCES `MyDatabase`.`UserProfile` (
`ProfileId`
) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE = INNODB;