php - 在外键约束“followers_ibfk_1”中引用列“follower_id”和引用列“id”不兼容。我的错误是什么?
问题描述
我正在尝试将 follower_id 连接到用户 id,而我得到的只是这个错误。谁能帮忙。这是我的代码:
Schema::create('followers', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger("user_id");
$table->foreignId("user_id")->constrainted()->onDelete();
$table->unsignedBigInteger("follower_id");
$table->foreignId("follower_id")->constrainted()->onDelete();
$table->timestamps();
});
}
$table->id();
$table->string("username");
$table->string("email");
$table->string("password");
$table->timestamps();
});
}
解决方案
follower_id
要将表引用users
为外键,请尝试以下操作:
$table->unsignedBigInteger('follower_id');
$table->foreign('follower_id')->references('id')->on('users');
警告:
指令:
$table->foreignId("user_id")->constrainted();
// OR
$table->foreignId("follower_id")->constrainted();
是一个快捷方式:
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users');
// OR
$table->unsignedBigInteger('follower_id');
$table->foreign('follower_id')->references('id')->on('followers');
https://laravel.com/docs/8.x/migrations#foreign-key-constraints
推荐阅读
- sql - 当日期为时间戳时,在指定日期之间抓取记录
- verilog - 时钟块层次结构中的信号延迟与波形中的 RTL 层次结构
- c++ - 为什么这个 SQL 查询似乎没有失败,但实际上失败了?
- javascript - Is there is a way to check a word is same to another word?
- python - PGAdmin4 虚拟主机不适用于 Apache2 + WSGI
- r - 如何对齐闪亮的输入框,特别是 selectInput 和 numericInput
- html - 如何使用 CSS 将搜索图标 (
- c# - 泛型和重载多态
- javascript - React Hooks:如何访问自定义属性值?
- c# - 如何解决字符串上可为空的 C# SqlNull 异常?