Laravel中错误地形成了外键约束

时间:2017-10-25 02:23:16

标签: laravel

我有几对表Client,Rooms和Reservations。当我尝试迁移时,我遇到了这种错误:

[Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1005 Can't create table `laravel_london`.`#sql-142c_6f` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table
   `reservations` add constraint `reservations_client_id_foreign` foreign key (`client_id`) references `clients` (`id`))



  [PDOException]
  SQLSTATE[HY000]: General error: 1005 Can't create table `laravel_london`.`#sql-142c_6f` (errno: 150 "Foreign key constraint is incorrectly formed")

这是我的预订迁移代码:

public function up()
{
    Schema::create('reservations', function (Blueprint $table) {
        $table->increments('id');
        $table->date('date_in');
        $table->date('date_out');
        $table->integer('client_id')->unsingned;
        $table->foreign('client_id')->references('id')->on('clients');
        $table->integer('room_id')->unsingned;
        $table->foreign('room_id')->references('id')->on('rooms');
        $table->timestamps();
    });
}
有些人说这是因为我的迁移顺序,但我认为它已经是正确的顺序,这里是它的截图:

enter image description here

那么如何才能在没有任何错误的情况下进行迁移呢?感谢。

1 个答案:

答案 0 :(得分:0)

您的拼写错误.. unsigned而非unsingned而您忘记使用()

public function up()
{
Schema::create('reservations', function (Blueprint $table) {
    $table->increments('id');
    $table->date('date_in');
    $table->date('date_out');
    $table->integer('client_id')->unsigned();
    $table->foreign('client_id')->references('id')->on('clients');
    $table->integer('room_id')->unsigned();
    $table->foreign('room_id')->references('id')->on('rooms');
    $table->timestamps();
});
}