[英]Laravel 6: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
[英]SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (Laravel)
我不斷收到此錯誤。 我知道這是外鍵的問題,但我真的無法理解它不能正常工作的原因。 我正在嘗試與橋接表建立多對多關系。 請,如果你能幫我解決這個問題! 在這里你可以看到遷移:
餐桌禮服:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Dress extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('dresses', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('dress_id');
$table->string('name')->unique();
$table->string('brand');
$table->decimal('price');
$table->string('fabric');
$table->string('size');
$table->string('type');
$table->string('wash_instruction');
$table->string('product_details');
$table->string('fit');
$table->rememberToken();;
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
表願望清單:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Wishlist extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('wishlists', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('wishlist_id');
$table->foreign('wishlist_id')
->references('user_id')
->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
橋表:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateWishlistDressTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('wishlist_dress', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('dress_id');
$table->foreign('dress_id')
->references('id')
->on('dresses')->onDelete('cascade');
$table->unsignedBigInteger('wishlist_id');
$table->foreign('wishlist_id')
->references('id')
->on('wishlists')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('wishlist_dress');
}
}
您的用戶遷移是如何設置的? 用戶遷移文件中有名為 users_id 的列嗎?
通常只使用 id 列,它是從 users 遷移文件中的$table->id()
生成的。 我相信如果您從users_id
更改為id
,您的遷移應該可以工作,如下所示:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Wishlist extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('wishlists', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('wishlist_id');
$table->foreign('wishlist_id')
->references('id') //Changed from user_id to id
->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.