是什么导致迁移失败并出现错误“只能在自动增量列上”

我的表帖子没有任何限制,我默认Laravel选择自动增量。它仍然失败,并显示“只能在自动增量列上”。


移民

public function up()

{

    Schema::create('posts', function (Blueprint $table) {

        $table->id();

        $table->id('user_id')->nullable();

        $table->id('admin_id')->nullable();

        $table->string('about')->nullable();

        $table->decimal('price')->nullable();

        $table->string('new_used')->nullable();

        $table->string('negotiable')->nullable();

        $table->dateTime('expire_date')->nullabe();


      

        $table->timestamps();

    });

}


aluckdog
浏览 37回答 2
2回答

跃然一笑

您不能有多个自动递增列。将外键类型更改为unsignedInteger(),它应该可以工作。这通常是我创建 id 列加上两个外键的方式。$table->increments('id');$table->unsignedInteger('user_id')->nullable();$table->unsignedInteger('admin_id')->nullable();或者,您也可以创建外键引用。$table->foreign('user_id')->references('id')->on('users');$table->foreign('admin_id')->references('id')->on('users');

守着一只汪

idon 方法是Blueprint一个别名,用于bigIncrements创建无符号大整数自动增量字段。因此,该表的迁移中有 3 个自动增量字段,但只能有 1 个。
打开App,查看更多内容
随时随地看视频慕课网APP