2015-12-03 230 views
1

我想用這段代碼創建一個表。如何使自動增量列非自動增量?

public function up() 
{ 
    Schema::create('BookInfo', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->integer('bookId',11); 
     $table->string('Name',255); 
     $table->string('Publisher')->nullable(); 
     $table->integer('Publishing_year',4)->nullable(); 
     $table->integer('Price',5)->nullable(); 
     $table->string('Language',30); 
     $table->timestamps(); 
    }); 
} 

當我試着php artisan migrate它顯示我這個錯誤。

[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key (SQL: create table
的BookInfo ( BOOKID int not null auto_increment primary key,名稱varchar(255) not null, 出版商varchar(255) null, Publishing_year int null auto_increment primary key,價格int null auto_increment primary key, 語言varchar(30) not null, created_at timestamp default 0 not null,的updated_at timestamp default 0 not null) default character set utf8 collate utf8_unicode_ci)

[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key

看來laravel通吃整列,自動遞增。這裏發生了什麼事實,而且t會是解決方案嗎?

回答

2

$table->integer('bookId',11);在語法上不正確在雄辯ORM(你不能設置任何大小limi t爲整數),這是你的情況造成的錯誤。

$table->increments('id');自動將id設置爲primary key

在Eloquent ORM中查找所有必要的表格命令:http://laravel.com/docs/4.2/schema#adding-columns

2

錯誤說,這一切,

$table->integer('bookId',11); 
          ^// this is considered as autoincrement rather than length 

沒有爲整數

沒有長度的選項查看SO this

參考,thisthis