2015-12-03 135 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