提问者:小点点

我有一个this error en laravel:语法错误或访问冲突:1075不正确的表定义。如何解决这个问题?


** SQLSTATE[42000]:语法错误或访问冲突:1075不正确的表定义;只能有一个自动列,并且必须将其定义为键(SQL:create table < code > loans (< code > id _ loan int unsigned not null auto _ increment主键,< code > book _ id bigint unsigned not null auto _ increment主键,< code > user _ id bigint unsigned not null auto _ increment主键,< code > date _ vto date not null)默认字符集utf8mb 4 collate ' utf8mb 4 b 4 _ 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);
    }

共1个答案

匿名用户

这个错误是不言自明的:

    $table->increments('id_loan');
    $table->bigIncrements('book_id')->unsigned();
    $table->bigIncrements('user_id')->unsigned();

您正在数据库中添加多个自动增量数据类型。您可以做的是使用相同的数据类型,但不是递增的,对于< code>bigIncrements,它是< code > unsignedbigininteger 。

您可以在文档中清楚地看到:

bigIncrements方法创建一个自动递增的无符号BIGINT(主键)等效列

$table->unsignedBigInteger('book_id')->unsigned();
$table->unsignedBigInteger('user_id')->unsigned();

在迁移中替换,然后重试。