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

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

当前回答

这是前面所说内容的微妙版本,但在我的实例中,我有2个数据库(foo和bar)。我首先创建了foo,我没有意识到它在酒吧引用了一个外键。Baz(尚未创建)。当我尝试创建bar的时候。baz(没有任何外键),我一直得到这个错误。在四处寻找了一会儿之后,我在foo中找到了外键。

所以,长话短说,如果您得到这个错误,您可能有一个预先存在的外键到正在创建的表。

其他回答

对我来说,1215错误发生在我导入mysqldump创建的dumpfile时,它按字母顺序创建了表,在我的例子中,这导致外键引用文件中稍后创建的表。(这篇博客文章指出:MySQL错误代码1215:“不能添加外键约束”)

由于mysqldump订单表字母顺序和我不想改变表的名字,我遵循的答案由JeremyWeir在本页上的说明,这说明要把设置FOREIGN_KEY_CHECKS = 0;把SET FOREIGN_KEY_CHECKS = 1;在转储文件的底部。

这个方法对我很有效。

所以我尝试了以上所有的修复,但运气不好。我可能错过了我的表中的错误-只是找不到原因,我一直得到错误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.

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

当使用Laravel迁移时尝试创建外键时,就像下面的例子:

用户表

public function up()
{
    Schema::create('flights', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->TinyInteger('color_id')->unsigned();
        $table->foreign('color_id')->references('id')->on('colors');
        $table->timestamps();
    });
}

颜色表

public function up()
{
    Schema::create('flights', function (Blueprint $table) {
        $table->increments('id');
        $table->string('color');
        $table->timestamps();
    });
}

有时候属性不起作用:

[PDOException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

发生此错误是因为[用户表]中的外键(类型)与[颜色表]中的主键(类型)不同。

要解决这个问题,你应该改变[colors table]中的主键:

($ table - > tinyIncrements’id’);


当你使用主键$table->Increments('id');时,你应该使用Integer作为外键:

$table->unsignedInteger('fk_id');
$table->foreign('fk_id')->references('id')->on('table_name');

$table->tinyIncrements('id');你应该使用unsignedTinyInteger作为外键:

$table->unsignedTinyInteger('fk_id');
$table->foreign('fk_id')->references('id')->on('table_name');

当你使用主键$table->smallIncrements('id');你应该使用unsignedSmallInteger作为外键:

$table->unsignedSmallInteger('fk_id');
$table->foreign('fk_id')->references('id')->on('table_name');

当你使用主键$table->mediumIncrements('id');你应该使用unsignedMediumInteger作为外键:

$table->unsignedMediumInteger('fk_id');
$table->foreign('fk_id')->references('id')->on('table_name');

这是前面所说内容的微妙版本,但在我的实例中,我有2个数据库(foo和bar)。我首先创建了foo,我没有意识到它在酒吧引用了一个外键。Baz(尚未创建)。当我尝试创建bar的时候。baz(没有任何外键),我一直得到这个错误。在四处寻找了一会儿之后,我在foo中找到了外键。

所以,长话短说,如果您得到这个错误,您可能有一个预先存在的外键到正在创建的表。

对于其他人,相同的错误可能并不总是由于列类型不匹配。您可以通过发出这个命令找到关于MySQL外键错误的更多信息

SHOW ENGINE INNODB STATUS;

您可能会在打印的消息顶部附近发现一个错误。类似的

属性所在的引用表中找不到索引 引用的列显示为第一个列或列类型 在表和引用表中不匹配约束。