2016-05-18 86 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) 

我已經嘗試了鍵約束聲明移到表創建結束後,到一個單獨的模式::表()塊,使外鍵可空()(然後逆轉該更改)並且翻轉了遷移的順序,但錯誤仍然存​​在。遷移運行(還有其他的來的,堅果沒有外鍵),直到服務表,然後我得到上述錯誤。

什麼是真的令我費解的是,我已經在同一臺服務器上運行過這些相同的遷移(不久之前),沒有問題。

在此先感謝您的幫助。

+0

這並不是說問題相當重複的,但我找到了答案在線。數據庫中已經存在一個約束,新遷移試圖使用的名稱。謝謝! – rodericktech

回答

0

如果欺騙不能解決問題,你可以隨時徹底關閉外鍵檢查遷移:

public function up() 
    DB::statement("SET FOREIGN_KEY_CHECKS = 0"); 
    Schema::create("services", ...); 
    DB::statement("SET FOREIGN_KEY_CHECKS = 1"); 
}