Laravel 6: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

Laravel 6: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

嘿,我目前正在进行 Laravel 6 数据库迁移,但是当我执行 php artisan migrate:fresh 时,出现以下错误: SQLSTATE[HY000]: 一般错误: 1215 无法添加外键约束 (SQL: alter table category_post add constraint category_post_category_id_foreign foreign key (category_id) references id (categories) on delete cascade on update cascade)

我检查了以下内容:

这是我的迁移代码我希望你能看到我犯的错误,因为我找不到它。

        // Table for storing Blog Posts
        Schema::create('posts', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->string('slug');
            $table->string('read_time');
            $table->string('summary');
            $table->string('body');
            /*$table->timestamps('created_at');
            $table->timestamps('updated_at');*/
            $table->timestamps();
        });

        // Table for storing categories
        Schema::create('categories', function (Blueprint $table) {
          $table->bigIncrements('id');
          $table->string('name')->unique();
          $table->string('slug')->unique();
          $table->string('description');
          $table->timestamps();
        });

        // Table for association of Categories with Blog Posts
        Schema::create('category_post', function (Blueprint $table) {
          $table->bigInteger('category_id');
          $table->bigInteger('post_id');

          $table->foreign('category_id')->references('id')->on('categories')
            ->onUpdate('cascade')->onDelete('cascade');
          $table->foreign('post_id')->references('id')->on('posts')
            ->onUpdate('cascade')->onDelete('cascade');

          $table->primary(['post_id', 'category_id']);
        });

        // Table for association of Users with Blog Posts
        Schema::create('user_post', function (Blueprint $table) {
          $table->bigInteger('user_id');
          $table->bigInteger('post_id');

          $table->foreign('user_id')->references('id')->on('users')
            ->onUpdate('cascade')->onDelete('cascade');
          $table->foreign('post_id')->references('id')->on('posts')
            ->onUpdate('cascade')->onDelete('cascade');

          $table->primary(['user_id', 'post_id']);
        });

最终目标是将类别和用户与帖子相关联,这样我就可以在帖子上添加标签和用户。

提前感谢您的帮助和善意,我对 Laravel 还很陌生,但我对 PHP 有很好的理解,所以如果您能解释一下,那就太好了我做错了:)

根据documentationbigIncrements表示:

Auto-incrementing UNSIGNED BIGINT (primary key) equivalent column.

因此,您的外键需要匹配 (unsignedBigInteger())。