2017-05-30 26 views
0

私は移行をリフレッシュしようとしています。しかし、php artisan migrate:refreshコマンドが機能していません。 それは次のエラーを示していますLaravel: 'dropForeign'メソッドを使用しても外部キーを削除できません

[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'classes_userid_foreign'; check that column/key exists (SQL: alter table classes drop foreign key classes_userid_foreign)

[PDOException] SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'classes_userid_foreign'; check that column/key
exists

私もdropForeignを使用しますが、その私のために働いていません。

私はこの問題を解決するためにどのようmigrationファイル

public function up() 
{ 
    Schema::create('classes', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('userId')->unsigned(); 
     $table->string('title'); 
     $table->string('name'); 
     $table->string('code')->unique(); 
     $table->integer('capacity')->unsigned(); 
     $table->integer('tagId')->unsigned(); 
     $table->foreign('userId')->references('id')->on('users'); 
     $table->timestamps(); 
    }); 
} 

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

を示していますか?

+0

表を確認しましたし、外部キーが存在することを確認しますか? – user3158900

答えて

0

ほとんどの場合、laravel(上または下のいずれか)で移行を実行し、行に沿ってエラーが発生すると、トランザクションがmigrationsテーブルに登録されずに部分的に完了することがあります。このような場合は、またはあなたが本当に外部キーが存在するがかどうか、テーブルをドロップするようになるならば、あなたはそのような外部キーチェックを無効にすることを検討すべきである

public function down() 
{ 
    Schema::disableForeignKeyConstraints(); 
    Schema::drop('classes'); 
    Schema::enableForeignKeyConstraints(); 
} 
関連する問題