Php Laravel迁移不会添加外键

Php Laravel迁移不会添加外键,php,laravel,Php,Laravel,我不熟悉迁移,尝试创建两个表,其中一个表中有一个外键,另一个表中引用了一个id,但我遇到了添加键的一般性失败错误。我有什么遗漏吗 错误: [pdo异常] SQLSTATE[HY000]:一般错误:1215无法添加外键约束 代码: 必须首先创建表,然后创建外键: Schema::create('app_to_bucket', function($table) { $table->increments('id'); $table->integer('bucket_id')

我不熟悉迁移,尝试创建两个表,其中一个表中有一个外键,另一个表中引用了一个id,但我遇到了添加键的一般性失败错误。我有什么遗漏吗

错误:

[pdo异常]
SQLSTATE[HY000]:一般错误:1215无法添加外键约束

代码:


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

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');
});
试试这个:

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');
  });

我已经解决了这个问题

问题在于,Laravel自动将递增列作为主键。所以我需要指定我的
app\u group\u 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');
  });

这肯定会奏效。雄辩的主键是长度为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');

外键和参考键应具有相同的长度和类型。如果设置的键满足此条件,则不会弹出错误:)

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

首先,您应该检查指定的列和表的名称

其次,在创建约束时检查数据库引擎。 请参阅文档,它应该是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');
})
;}

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

我尝试过这一点,但得到了相同的错误…更新了上面的代码只是不创建列,它将由您的数据库服务器添加。已编辑。我尝试删除该行,但它抛出一个错误,表示“app\u group\u id”不存在于表中这是哪个数据库?尝试像上次编辑一样使用unsigned()。奇怪,应该可以。关于这一点,这里有另一个公认的答案:。为什么在创建表时不添加外键?啊,我错了,这将不起作用…我需要应用程序组id能够重复它不能递增。可以是具有相同应用程序组id的多个应用程序名称。我已将“组id”添加到“应用程序组”。我在我的应用程序上有几乎完全相同的代码,它工作正常。我复制粘贴了这个代码,但它不工作。当我尝试引用递增的id而不是组时,它可以正常工作。您可以尝试将代码包装到:DB::statement('SET FOREIGN_KEY_CHECKS=0')//这里的代码DB::语句('SET FOREIGN_KEY_CHECKS=1');看看发生了什么事,很完美,谢谢。更仔细地看,文档说:“注意:当创建引用递增整数的外键时,请记住始终使外键列无符号。”(ref:)只是好奇,整数的默认长度是多少?长度(10)部分是必需的吗?MySQL中整数的默认长度是11,但在Laravel中是10。我不知道为什么。也许是有原因的。它开始工作时没有任何长度(10)。我用谷歌搜索了5个小时,终于找到了你的解决方案,救了我一天。谢谢
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');
})
;}