**SQLSTATE[42000]:语法错误或访问冲突:1075表定义不正确;只能有一个auto列,并且必须将其定义为键(SQL:create tableloans
(id_loan
int unsigned not null auto_increment主键、book_id
bigint unsigned not null auto_increment主键、user_id
bigint signed not null autom_increment primary key、date_vto
date not null(默认字符集utf8mb4 collate'utf8mb04_unicode_ci'(。如何解决此问题?
**
迁移
public function up()
{
Schema::create('books', function (Blueprint $table) {
$table->increments('id_book');
$table->string('title');
$table->integer('amount');
});
}
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('username');
$table->string('password');
$table->string('role');
});
}
public function up()
{
Schema::create('loans', function (Blueprint $table) {
$table->increments('id_loan');
$table->bigIncrements('book_id')->unsigned();
$table->bigIncrements('user_id')->unsigned();
$table->date('date_vto');
$table->foreign('book_id')->references('id_book')->on('books')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
型号书籍
public function loans(){
return $this->hasMany(Loans::class);
}
模型贷款
public function books()
{
return $this->belongsTo(Books::class, 'book_id', 'id_book');
}
public function users()
{
return $this->belongsTo(Users::class, 'user_id', 'id');
}
模型用户
public function loans(){
return $this->hasMany(Loans::class);
}
这个错误不言自明:
$table->increments('id_loan'); $table->bigIncrements('book_id')->unsigned(); $table->bigIncrements('user_id')->unsigned();
您正在数据库中添加多个自动递增数据类型。您可以使用相同的数据类型,但不递增,对于bigIncrements
,它是unsignedBigInteger
。
你可以在文件中清楚地看到:
bigIncrements方法创建一个自动递增的未签名BIGINT(主键(等价列
$table->unsignedBigInteger('book_id')->unsigned();
$table->unsignedBigInteger('user_id')->unsigned();
请在迁移中替换,然后重试。