2016-12-02 28 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
  • ...,中,大。