如何使自动增量列非自动增量?

时间:2015-12-03 06:19:05

标签: php mysql laravel-5

我正在尝试使用此代码创建一个表。

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将所有整数列作为自动增量。实际上这里发生了什么以及解决方案是什么?

2 个答案:

答案 0 :(得分:3)

错误说明了一切,

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

整数没有长度选项

请参阅this

参考SO,thisthis

答案 1 :(得分:2)

在Eloquent ORM中

$table->integer('bookId',11);在语法上不正确(您无法设置整数的任何大小限制),这会导致您的错误。

$table->increments('id');会自动将id设为primary key

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