I keep getting this error. I understood it's a problem with the foreign key but i really can't understand what it's not working properly. I'm trying to make a many to many relationship with a bridge table. Please, if you can help me solve this! Here you can see the migrations:
Table dresses:
<?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()
{
//
}
}
Table Wishlist:
<?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()
{
//
}
}
Bridge table:
<?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');
}
}
CodePudding user response:
How is your users migration set up? Do you have a column named users_id in the users migration file?
Usually, only the id column is used, which is generated from $table->id()
in the users migrations file. I believe your migrations should work if you change from users_id
to id
like following:
<?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()
{
//
}
}