SQLSTATE[42000]:语法错误或访问冲突:1075 table 定义不正确;

SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition;

迁移文件

$table->increments('id');
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('band_id')->references('id')->on('bands');
$table->foreign('genre_id')->references('id')->on('genres');
$table->foreign('cate_id')->references('id')->on('cates');
$table->foreign('type_id')->references('id')->on('types');
$table->integer('status');
$table->date('date');
$table->time('time');
$table->decimal('price');
$table->tinyIncrements('instrument');
$table->string('instrument_detail',255);
$table->timestamps();

运行phpartisan迁移后

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 bookings (id int unsigned not null auto_increment primary key, status int not null, date date not null, time time not null, price decimal(8, 2) not null, instrument tinyint unsigned not null auto_increment primary key, instrument_detail varchar(255) not null, created_at timestamp null, updated_at timestamp null) default character set utf8mb4 collate utf8mb4_unicode_ci)

下面这个

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

$table->unsignedTinyInteger('instrument', true);

第二个参数是 bool,用于自动递增,默认为 false

下一句:

$table->foreign('user_id')->references('id')->on('users');

只是告诉数据库在 parent/foreign 列之间创建一个 link,但为了做到这一点,该列必须先前存在,所以:

$table->unsignedInteger('user_id'); // first this
$table->foreign('user_id')->references('id')->on('users'); // then this

您应该为每个外键执行此操作。

注:

Laravel 不需要你定义这个 links 因为它没有使用它,只是为了数据库的一致性。