我试图将我的新模式转发到我的数据库服务器上,但我不知道为什么我会得到这个错误。

我试图在这里寻找答案,但我所找到的一切都表明,要么将数据库引擎设置为InnoDB,要么确保我试图用作外键的键是它们自己表中的主键。如果我没记错的话,这两件事我都做过。我还能做什么?

Executing SQL script in server

ERROR: Error 1215: Cannot add foreign key constraint

-- -----------------------------------------------------
-- Table `Alternative_Pathways`.`Clients_has_Staff`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `Alternative_Pathways`.`Clients_has_Staff` (
  `Clients_Case_Number` INT NOT NULL ,
  `Staff_Emp_ID` INT NOT NULL ,
  PRIMARY KEY (`Clients_Case_Number`, `Staff_Emp_ID`) ,
  INDEX `fk_Clients_has_Staff_Staff1_idx` (`Staff_Emp_ID` ASC) ,
  INDEX `fk_Clients_has_Staff_Clients_idx` (`Clients_Case_Number` ASC) ,
  CONSTRAINT `fk_Clients_has_Staff_Clients`
    FOREIGN KEY (`Clients_Case_Number` )
    REFERENCES `Alternative_Pathways`.`Clients` (`Case_Number` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_Clients_has_Staff_Staff1`
    FOREIGN KEY (`Staff_Emp_ID` )
    REFERENCES `Alternative_Pathways`.`Staff` (`Emp_ID` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB

SQL脚本执行完成:语句:7成功,1失败

下面是父表的SQL。

CREATE  TABLE IF NOT EXISTS `Alternative_Pathways`.`Clients` (
  `Case_Number` INT NOT NULL ,
  `First_Name` CHAR(10) NULL ,
  `Middle_Name` CHAR(10) NULL ,
  `Last_Name` CHAR(10) NULL ,
  `Address` CHAR(50) NULL ,
  `Phone_Number` INT(10) NULL ,
  PRIMARY KEY (`Case_Number`) )
ENGINE = InnoDB

CREATE  TABLE IF NOT EXISTS `Alternative_Pathways`.`Staff` (
  `Emp_ID` INT NOT NULL ,
  `First_Name` CHAR(10) NULL ,
  `Middle_Name` CHAR(10) NULL ,
  `Last_Name` CHAR(10) NULL ,
  PRIMARY KEY (`Emp_ID`) )
ENGINE = InnoDB

当前回答

在使用Laravel 4时,特别是使用JeffreyWay的Laravel 4生成器时,我遇到了一个“错误1215:不能添加外键约束”的陷阱。

在Laravel 4中,您可以使用JeffreyWay的Generators生成迁移文件,逐个创建表,这意味着每个迁移文件生成一个表。

您必须意识到这样一个事实,即每个迁移文件在文件名中都有一个时间戳,它给出了文件的顺序。生成的顺序也是当你触发Artisan CLI命令php Artisan migrate时迁移操作的顺序。

因此,如果一个文件请求一个指向后一个文件中将生成但尚未生成的键的外键约束,Error 1215将被触发。

在这种情况下,您需要调整迁移文件生成的顺序。按正确顺序生成新文件,复制内容,然后删除无序的旧文件。

其他回答

In my case, I had deleted a table using SET FOREIGN_KEY_CHECKS=0, then SET FOREIGN_KEY_CHECKS=1 after. When I went to reload the table, I got error 1215. The problem was there was another table in the database that had a foreign key to the table I had deleted and was reloading. Part of the reloading process involved changing a data type for one of the fields, which made the foreign key from the other table invalid, thus triggering error 1215. I resolved the problem by dropping and then reloading the other table with the new data type for the involved field.

我猜是客户。Case_Number和/或Staff。Emp_ID与Clients_has_Staff的数据类型不完全相同。Clients_Case_Number和Clients_has_Staff.Staff_Emp_ID。

也许父表中的列是INT UNSIGNED?

两个表中的数据类型必须完全相同。

我也遇到过同样的问题,我的解决方案是:

之前:

CREATE TABLE EMPRES
( NoFilm smallint NOT NULL

  PRIMARY KEY (NoFilm)

  FOREIGN KEY (NoFilm) REFERENCES cassettes

);

解决方案:

CREATE TABLE EMPRES
(NoFilm smallint NOT NULL REFERENCES cassettes,

 PRIMARY KEY (NoFilm)

);

所以我尝试了以上所有的修复,但运气不好。我可能错过了我的表中的错误-只是找不到原因,我一直得到错误1215。所以我用了这个方法。

In my local environment in phpMyAdmin, I exported data from the table in question. I selected format CSV. While still in phpMyAdmin with the table selected, I selected "More->Options". Here I scrolled down to "Copy table to (database.table). Select "Structure only". Rename the table something, maybe just add the word "copy" next to the current table name. Click "Go" This will create a new table. Export the new table and import it to the new or other server. I am also using phpMyAdmin here also. Once imported change the name of the table back to its original name. Select the new table, select import. For format select CSV. Uncheck "enable foreign key checks". Select "Go". So far all is working good.

我在博客上发布了我的解决方案。

我经历这个错误的原因完全不同。我使用MySQL Workbench 6.3创建我的数据模型(很棒的工具)。我注意到,当外键约束定义中定义的列顺序不符合表列顺序时,也会生成此错误。

我花了大约4个小时的时间尝试了所有其他方法,就是没能确认。

现在一切正常,我可以继续编码了。:-)