2016-12-02 56 views
0

我想打表遷移, 但是當我執行有錯誤按摩用戶ID成爲主鍵和自動增量由它的自我

Incorrect table definition; there can be only one auto column and it must be defined as key 

enter image description here

,但我沒有設置用戶ID爲增量和主鍵,爲什麼?

我是一個初學者反正 這裏是我的代碼

Schema::create('orders', function(Blueprint $table){ 

      $table->increments('order_id'); 
      $table->dateTime('order_date'); 
      $table->integer('order_status')->default(0); 
      $table->integer('user_id', 10)->unsigned(); 
      $table->string('npk_user_approval', 10); 
      $table->string('npk_ga_approval', 10); 
      $table->dateTime('approval_user_at')->nullable(); 
      $table->dateTime('approval_ga_at')->nullable(); 
      $table->dateTime('reject_user_at')->nullable(); 
      $table->dateTime('reject_ga_at')->nullable(); 
      $table->dateTime('created_at')->nullable(); 
      $table->string('created_by', 50)->nullable(); 
      $table->dateTime('updated_at')->nullable(); 
      $table->string('updated_by', 50)->nullable(); 

      $table->foreign('user_id')->references('id')->on('users'); 
     }); 
+0

您使用的是哪個版本的Laravel? – Gadzhev

+0

@Gadzhev我使用的是5.3版本 –

回答

1

藍圖的integer()方法的第二個參數是$autoIncrement?。您傳遞的是10,實際上它設置爲自動遞增。下面是來自Blueprint類所採取的代碼:

/** 
* Create a new integer (4-byte) column on the table. 
* 
* @param string $column 
* @param bool $autoIncrement 
* @param bool $unsigned 
* @return \Illuminate\Support\Fluent 
*/ 
public function integer($column, $autoIncrement = false, $unsigned = false) 
{ 
    return $this->addColumn('integer', $column, compact('autoIncrement', 'unsigned')); 
} 

因此,而不是經過10(你認爲是大小),而是執行此操作:

$table->unsignedInteger('user_id'); 

相當於創建整數列,無符號,autoIncrement關閉。

要更改整數尺寸,使用的方法,如

  • tinyInteger
  • unsignedTinyInteger
  • smallInteger
  • unsignedSmallInteger
  • ...,中,大。