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

我试图在这里寻找答案,但我所找到的一切都表明,要么将数据库引擎设置为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将被触发。

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

其他回答

此错误的另一个来源是当您有两个或多个具有相同外键名的相同表名时。

这种情况有时会发生在使用建模和设计软件(如MySQL Workbench),然后从设计中生成脚本的人身上。

当列的类型不相同时也会发生这种情况。

例如,如果你引用的列是一个UNSIGNED INT,而被引用的列是INT,那么你会得到这个错误。

在我的情况下,我不得不禁用外键检查,因为源表不存在。

设置FOREIGN_KEY_CHECKS = 0;

你可能会得到外键约束错误的原因:

You are not using InnoDB as the engine on all tables. You are trying to reference a nonexistent key on the target table. Make sure it is a key on the other table (it can be a primary or unique key, or just a key) The types of the columns are not the same (an exception is the column on the referencing table can be nullable even if it is not nullable in the referenced table). If the primary key or foreign key is a varchar, make sure the collation is the same for both. One of the reasons may also be that the column you are using for ON DELETE SET NULL is not defined to be null. So make sure that the column is set default null.

检查这些。

另一个原因:如果你使用ON DELETE SET NULL,所有外键中的列都必须允许空值。有人在这个问题中发现了这一点。

从我的理解,这不会是一个关于数据完整性的问题,但似乎MySQL只是不支持这个功能(在5.7)。