gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 01:48:32 33 4
gpt4 key购买 nike

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

我检查了以下内容:

  • 错别字
  • 引用顺序错误
  • 从 Laravel 5.8 开始从 increment 更改为 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/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com