php - 拉拉维尔 6 : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

标签 php mysql sql laravel-6

嘿,我目前正在进行 Laravel 6 数据库迁移,但是当我执行 php artisan migrate:fresh 时,我会看到以下错误: SQLSTATE[HY000]:一般错误:1215 无法添加外键约束(SQL:alter table category_post 添加约束 category_post_category_id_foreign 外键(category_id >) 在删除级联和更新级联上引用id(categories)

我检查了以下内容:

  • 错别字
  • 引用顺序错误
  • 自 Laravel 5.8 起从增量更改为 bigIncrement
  • 类型错误

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

        // 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 有很好的了解,所以如果您能解释我做错了什么,那就太好了:)

最佳答案

根据documentation , bigIncrements 代表:

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

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

关于php - 拉拉维尔 6 : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57902287/

相关文章:

mysql - (SQL 帮助)有人可以告诉我我在哪里出错了吗?

php数据库在网页中显示

PHP 生成 13 位时间戳,如 mktime

mysql - sql中insert语句中的select语句返回多行

java - 奇怪的 org.springframework.jdbc.BadSqlGrammarException

sql - 如何从 SQL Server 中删除图表支持对象?

php - SQL 和 PHP - mysqli_num_rows() 和 'select count()' 哪个更快?

php目录遍历问题

PHP7 MySQL 'expiry_date' PDO 数据库安装的默认值无效

php - Laravel Eloquent 向上投票和向下投票