2016-03-08 110 views
2

良好的一天,我有兩個遷移表,第一個是用於創建客戶模式:Laravel 5.2 [PDOException] SQLSTATE [42000] - 創建兩個AUTO_INCREMENT主鍵字段

Schema::create('customers', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('first_name',45); 
     $table->string('middle_name',45)->nullable(); 
     $table->string('last_name',45); 
     $table->string('gender',45); 
     $table->string('dob',45); 
     $table->string('martial_status',45); 
     $table->string('home_phone',12)->nullable(); 
     $table->string('mobile_phone',12); 
     $table->string('work_phone',12); 
     $table->string('id_number',12); 
     $table->string('id_type',45); 
     $table->string('id_exp',20); 
     $table->date('id_issue_date'); 
     $table->text('id_image')->nullable(); 
     $table->timestamps(); 

第二個是用於創建customer_address模式:

Schema::create('customers_address', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('customer_id')->unsigned(); 
     $table->foreign('customer_id')->references('id')->on('customers')->onDelete('cascade'); 
     $table->string('address_line1',20); 
     $table->string('address_line2',20)->nullable(); 
     $table->string('ownership',20); 
     $table->string('country'); 
     $table->string('town_city'); 
     $table->string('parish_state'); 
     $table->integer('years_at_address',10); 
     $table->timestamps(); 
    }); 
運行PHP工匠遷移時

,我得到了我的終端一個錯誤,說[PDOException] SQLSTATE [42000]:語法錯誤或訪問衝突:1075不正確的表定義;只能有一個自動列,並且必須將其定義爲鍵。

什麼可能導致我得到這個,因爲我沒有看到我在這裏做錯了什麼。在此先感謝您的幫助。

回答

1

$table->integer('years_at_address',10);應該是$table->integer('years_at_address');第二個參數是布爾值,如果爲真,則將整數計算爲主鍵。

這是整函數怎麼看起來像Laravel框架中BluePrint類:

public function integer($column, $autoIncrement = false, $unsigned = false) 
{ 
    return $this->addColumn('integer', $column, compact('autoIncrement', 'unsigned')); 
} 

所以將其設置爲10,你告訴給你希望此列被視爲autoIncrementLaravel遷移功能並最終成爲主要關鍵。

+0

謝謝,不敢相信我沒有看到。 – newbieDev

相關問題