我正在尝试使用此代码创建一个表。
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]
的BookInfo
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(
BOOKIDint not null auto_increment primary key,
名称varchar(255) not null,
发布商varchar(255) null,
Publishing_yearint null auto_increment primary key,
价格int null auto_increment primary key,
语言varchar(30) not null,
created_attimestamp default 0 not null,
updated_attimestamp 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将所有整数列作为自动增量。实际上这里发生了什么以及解决方案是什么?
答案 0 :(得分:3)
错误说明了一切,
$table->integer('bookId',11);
^// this is considered as autoincrement rather than length
整数没有长度选项
请参阅this
答案 1 :(得分:2)
$table->integer('bookId',11);
在语法上不正确(您无法设置整数的任何大小限制),这会导致您的错误。
$table->increments('id');
会自动将id
设为primary key
。
在此处查找Eloquent ORM中所有必要的表构建命令:http://laravel.com/docs/4.2/schema#adding-columns