2016-05-18 2 views
0

ここが問題です。私はLaravelでキー制約がどのように働くべきかをよく知っています。私はまた、この問題を徹底的に調べて、無駄に多くのソリューションを試しました。私はこのように見えるの移行のカップルを持っている、とあなたが見る順序で実行されていますLaravelでの外部キー制約の追加エラー - 正しい順序での移行

public function up() 
14 { 
15  Schema::create('customers', function (Blueprint $table) { 
16  $table->engine='InnoDB'; 
17  $table->increments('id'); 
18  $table->string('customer_first_name'); 
19  $table->string('customer_last_name'); 
20  $table->string('street_address_1'); 
21  $table->string('street_address_2'); 
22  $table->string('city'); 
23  $table->string('state', '2'); 
24  $table->integer('zip'); 
25  $table->string('region'); 
26  $table->string('customer_email')->unique(); 
27  $table->integer('added_by'); 
28  $table->timestamps(); 
29  }); 
30 } 

public function up() 
14 { 
15  Schema::create('services', function (Blueprint $table) { 
16  $table->engine='InnoDB'; 
17  $table->increments('id'); 
18  $table->integer('customer_id')->unsigned(); 
19  $table->foreign('customer_id') 
20   ->references('id')->on('customers')->onDelete('cascade'); 
21  $table->string('service'); 
22  $table->text('service_description'); 
23  $table->date('service_date'); 
24  $table->integer('reminder_sent'); 
25  $table->tinyInteger('review_posted'); 
26  $table->string('token'); 
27  $table->timestamps(); 
28  }); 
29 } 

私は最後の数以内に問題なくこれらの移行を実行したソフトウェアのインストール数を持っています月。しかし、今日インストールしようとすると、サービスの移行は、次のメッセージを(繰り返し)に失敗しました:

[Illuminate\Database\QueryException] 
SQLSTATE[HY000]: General error: 1005 Can't create table 'gen_hometown3 
.#sql-6ef_19b6c' (errno: 121) (SQL: alter table `sr_client_services` ad 
d constraint `services_customer_id_foreign` foreign key (`customer_id`) ref 
erences `sr_client_customers` (`id`) on delete cascade) 


[PDOException] 
SQLSTATE[HY000]: General error: 1005 Can't create table 'gen_hometown3 
.#sql-6ef_19b6c' (errno: 121) 

私は別のスキーマ::テーブルに、テーブル作成の最後にキー制約宣言を移動しようとしています()ブロックで、外部キーをnullable()にして(その変更を元に戻して)、移行の順序を逆転させましたが、エラーはそのまま残ります。マイグレーションは実行されます(前に来る他のものがあり、ナットには外部キーがありません)。サービステーブルまで、右に説明したエラーが表示されます。

とは本当に私は困っています。私はこの同じマイグレーションを、以前はまったく同じサーバー上で問題なく使用していました。

ご協力いただきありがとうございます。

+0

をそれはその質問のかなり重複しませんでしたが、私は中に答えを見つけました糸。新しいマイグレーションが使用しようとしていた名前の制約がすでにデータベースにありました。ありがとう! – rodericktech

答えて

0

デュープは、問題が解決しない場合、あなたは常に、移行のために完全に外部キーチェックを無効にすることができます

public function up() 
    DB::statement("SET FOREIGN_KEY_CHECKS = 0"); 
    Schema::create("services", ...); 
    DB::statement("SET FOREIGN_KEY_CHECKS = 1"); 
} 
関連する問題