2014-02-27 93 views
4

我是新来的迁移,并尝试创建2个表中的外键在一个引用另一个ID,但我得到一个通用失败,添加关键错误。有什么我失踪?Laravel迁移不会添加外键

错误:

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

代码:

Schema::create('app_groups', function($table) { 
    $table->increments('id'); 
    $table->string('app_name'); 
    $table->unsignedInteger('app_group_id'); 
    $table->timestamps(); 
    }); 

    Schema::create('app_to_bucket', function($table) { 
    $table->increments('id'); 
    $table->unsignedInteger('app_group_id'); 
    $table->unsignedInteger('bucket_id'); 
    $table->timestamps(); 
    }); 
    Schema::table('app_to_bucket', function($table) { 
    $table->foreign('app_group_id')->references('app_group_id')->on('app_groups')->onDelete('cascade'); 
    }); 
+0

为什么不在表创建时添加外键? – ollieread

回答

5

我已经解决了这个问题。

问题在于Laravel自动假设列增长为主键。所以我需要指定我的app_group_id是主键。

Schema::create('app_groups', function($table) { 
    $table->string('app_name'); 
    $table->integer('app_group_id'); 
    $table->primary('app_group_id'); 
    $table->timestamps(); 
    }); 

    Schema::create('app_to_bucket', function($table) { 
    $table->integer('app_group_id'); 
    $table->integer('bucket_id'); 
     $table->primary('bucket_id'); 
    $table->timestamps(); 
    }); 
    Schema::table('app_to_bucket', function($table) { 
    $table->foreign('app_group_id')->references('app_group_id')->on('app_groups')->onDelete('cascade'); 
    }); 
0

您必须先创建表,然后创建外键:

Schema::create('app_to_bucket', function($table) { 
    $table->increments('id'); 
    $table->integer('bucket_id')->unsigned(); 
    $table->integer('app_group_id')->unsigned(); 
    $table->timestamps(); 
}); 

Schema::table('app_to_bucket', function($table) { 
    $table->foreign('app_group_id')->references('app_group_id')->on('app_groups')->onDelete('cascade'); 
}); 
+0

我试过这个,得到了同样的错误...更新了上面的代码 – arrowill12

+0

只是不创建列,它会被数据库服务器添加。编辑。 –

+0

我试图删除该行,它会抛出一个错误,说'app_group_id'不存在于表 – arrowill12

0

试试这个:

Schema::create('app_groups', function($table) { 
    $table->increments('id'); 
    $table->integer('group_id')->unsigned(); 
    $table->string('app_name'); 
    $table->timestamps(); 
    }); 
Schema::create('app_to_bucket', function($table) { 
    $table->increments('id'); 
    $table->integer('app_group_id')->unsigned(); 
    $table->integer('bucket_id')->unsigned(); 
    $table->timestamps(); 
    $table->foreign('app_group_id')->references('group_id')->on('app_groups')->onDelete('cascade'); 
    }); 
+0

谢谢你,虽然我没有删除'蓝图' – arrowill12

+0

啊我误会这不会工作...我需要app_group_id能够重复它不能递增。可以是具有相同app_group_id的多个app_names – arrowill12

+0

我已将'group_id'添加到'app_groups'。我的应用程序中几乎有完全相同的代码,并且工作正常。 – Morgan

15

这将工作肯定。雄辩的主键是长度为10且无符号的整数。这就是关系不起作用的原因。

Schema::create('app_groups', function($table) { 
    $table->string('app_name'); 
    $table->integer('app_group_id')->length(10)->unsigned(); 
    $table->timestamps(); 
    }); 

    Schema::create('app_to_bucket', function($table) { 
    $table->integer('app_group_id'); 
    $table->integer('bucket_id')->length(10)->unsigned(); 
    $table->timestamps(); 
    }); 
    Schema::table('app_to_bucket', function($table) { 
    $table->foreign('app_group_id')->references('app_group_id')->on('app_groups')->onDelete('cascade'); 
+3

完美的作品,谢谢。更仔细地看,文档说:“注意:当创建一个引用递增整数的外键时,请记得始终使外键列无符号。” (ref:http://laravel.com/docs/schema) –

+0

只是好奇,什么是一个整数的默认长度?长度(10)部分是否必要? – Zuko

+0

MySQL中整数的默认长度是11,但在Laravel中是10。我不知道为什么。也许有一个原因。它没有长期的工作(10)。但我只是为了任何情况而使用它。 – TodStoychev

2

外键和引用键应具有相同的长度和相同的类型。如果你设置的键满足这个要求,错误将不会弹出:)

1

好的,在使用Laravel在MySQL数据库中创建/添加外键约束时,可能会遇到几个问题。

首先,您应该检查您分配的列和表的名称。

其次,在创建约束时检查数据库引擎。 请参阅文档https://dev.mysql.com/doc/refman/5.7/en/innodb-foreign-key-constraints.html它应该是InnoDB

Schema::create('app_groups', function($table) { 
    // setting up the storage engine 
    $table->engine='InnoDB'; 
    $table->increments('id'); 
    $table->integer('group_id')->unsigned(); 
    $table->string('app_name'); 
    $table->timestamps(); 
    }); 

Schema::create('app_to_bucket', function($table) { 
    $table->engine='InnoDB'; 
    $table->increments('id'); 
    $table->integer('app_group_id')->unsigned(); 
    $table->integer('bucket_id')->unsigned(); 
    $table->timestamps(); 
    $table->foreign('app_group_id') 
      ->references('group_id') 
      ->on('app_groups') 
      ->onDelete('cascade'); 
}) 
;} 

第三(可选),将您的约束(外键,索引等)的分配新建分配FY到单独的迁移