我有一個本地開發的帶有 sqlite 的 laravel 專案。出于部署原因,我想切換到 mysql。不幸的是,我的關系遷移不再起作用并產生以下錯誤(我已確保它們運行的??順序正確,所有其他必需的表首先生成并且看起來正確)
Can't create table `laraveltest`.`test1s_test2s` (errno: 150 "Foreign key constraint is incorrectly formed")
(SQL: alter table `test1s_test2s` add constraint `test1s_test2s_test1_id_foreign` foreign key (`suacap_id`)
references `test1s` (`id`) on delete cascade)
遷移如下所示:
測驗1
public function up()
{
Schema::create('test1s', function (Blueprint $table) {
$table->id();
...
測驗2
public function up()
{
Schema::create('test2s', function (Blueprint $table) {
$table->id();
...
關系表 test1s_test2s
public function up()
{
Schema::create('test1s_test2s', function (Blueprint $table) {
$table->primary(['test1_id', 'test2_id']);
$table->string('test1_id');
$table->foreign('test1_id')
->references('id')
->on('test1s')->onDelete('cascade');
$table->string('test2_id');
$table->foreign('test2_id')
->references('id')
->on('test2s')->onDelete('cascade');
});
}
我猜這與未簽名的主鍵有關,而其他表的 bigInt id 是?我嘗試修改
$table->primary(['test1_id', 'test2_id'])->unsigned();
但這不起作用。
有人可以指出我正確的方向嗎?謝謝
uj5u.com熱心網友回復:
在 Laravel 9 中,每當你制作外鍵時,它們應該是 UNSIGNED BIGINT 而不是字串 - $table->foreignId('user_id'); 另請閱讀官方檔案Laravel Official Doc
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/525239.html
上一篇:錯誤代碼:3819。違反檢查約束“checker_1”
下一篇:為什么MySQL不使用索引進行簡單的“SELECT*FROMTableWHEREfield='value'”查詢?
