【发布时间】:2014-03-31 10:00:30
【问题描述】:
我是迁移的新手,并试图创建 2 个表,其中一个引用了另一个 id 的外键,但我遇到了添加键错误的一般失败。有什么我想念的吗?
错误:
[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');
});
【问题讨论】:
-
建表时为什么不加外键?