2017-03-12 17 views
1

私はマイグレーションをいくつか行いました。マイグレーションを試みたときにエラーが発生しました。 私はどこで間違いを犯したのか分かりません。私はLaravelの移行を初めて知りました。Laravel 5.4マイグレーションエラー

以下

私の移行ファイルのコード

<?php 

use Illuminate\Support\Facades\Schema; 
use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateFavouritesTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('favourites', function (Blueprint $table) 
     { 
      $table->increments('id'); 
      $table->integer('user_id')->unsigned(); 
      $table->integer('name_id')->unsigned(); 
      $table->timestamps(); 
     }); 
     Schema::table('favourites', function(Blueprint $table) 
     { 
      $table->foreign('user_id')->references('id')->on('users')->onDelete('CASCADE'); 
      $table->foreign('name_id')->references('id')->on('names')->onDelete('CASCADE'); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::dropIfExists('favourites'); 
     Schema::table('favourites', function(Blueprint $table) 
     { 
      $table->dropForeign(['user_id']); 
      $table->dropForeign(['name_id']); 
     }); 
    } 
} 

ある `と私は移行する際、これは私が取得していますエラーです:

[Illuminate\Database\QueryException] 
    SQLSTATE[HY000]: General error: 1005 Can't create table `ugal`.`#sql-187c_52` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `favour 
    ites` add constraint `favourites_name_id_foreign` foreign key (`name_id`) references `names` (`id`) on delete CASCADE) 



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

は間違いで、なぜどこ?

答えて

2

favouritesテーブルの前にテーブルnamesを移行する必要があります。