我从我在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;
任何想法发生了什么?
EDIT ============================
表有两个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;
答案 0 :(得分:2)
如果
`UserId` INT NOT NULL AUTO_INCREMENT
在您的原始表中,您需要在派生表中使用完全相同的语法:
`UserId` VARCHAR( 45 ) NOT NULL
应该是
`UserId` INT NOT NULL
此外,您需要在两个表中索引字段UserId
。由于它是 PRIMARY ,所以不需要那里。在另一个补充:
INDEX `uid`( `UserId` )
答案 1 :(得分:0)
在
CREATE TABLE IF NOT EXISTS `myDatabase`.`UserProfile`
您引用数据库OurAgreement
中的表:
REFERENCES `OurAgreement`.`UserAccount`
但您之前创建的表UserAccount
位于mydatabase
:
CREATE TABLE IF NOT EXISTS `mydatabase`.`UserAccount`