You should have the accessories table migration running before the `bookings table migration so that the table exists to make the foreign key constraint?
Problem with migrations and relationship
Hi all, I have this problem: in my app I have a booking model with these relationships:
public function user()
{
return $this->belongsTo(User::class);
}
public function vespa()
{
return $this->hasOne(Vespa::class);
}
public function accessories()
{
return $this->hasMany(Accessory::class);
}
In Vespa model I have:
public function booking() { return $this->belongsTo(Booking::class); }
In Accessory model I have:
public function bookings() { return $this->belongsTo(Booking::class); }
This is the booking migration:
Schema::create('bookings', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned()->index(); $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade'); $table->integer('vespa_id')->nullable()->unsigned(); $table->foreign('vespa_id')->references('id')->on('vespas'); $table->integer('accessory_id')->nullable()->unsigned(); $table->foreign('accessory_id')->references('id')->on('accessories'); });
In vespa and accessory migration I didn't include the booking id but I created two different pivot tables.
This is booking_vespa:
Schema::create('booking_vespa', function (Blueprint $table) { $table->increments('id'); $table->integer('booking_id')->unsigned(); $table->integer('booking_id')->references('id')->on('bookings'); $table->integer('vespa_id')->unsigned(); $table->integer('vespa_id')->references('id')->on('vespas');
});
This is accessory_booking:
Schema::create('accessory_booking', function (Blueprint $table) { $table->increments('id'); $table->integer('accessory_id')->unsigned(); $table->integer('accessory_id')->references('id')->on('accessories'); $table->integer('booking_id')->unsigned(); $table->integer('booking_id')->references('id')->on('bookings'); });
As soon as I run the php artisan migrate command I have this error:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
: alter table bookings add constraint bookings_accessory_id_foreign for
eign key (accessory_id) references accessories (id))
[PDOException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
Any idea? Thanks in advance, Vincenzo
You need to use the foreign() method when defining the constraint, not integer:
Schema::create('booking_vespa', function (Blueprint $table) {
$table->increments('id');
$table->integer('booking_id')->unsigned();
$table->foreign('booking_id')->references('id')->on('bookings');
$table->integer('vespa_id')->unsigned();
$table->foreign('vespa_id')->references('id')->on('vespas');
});
Same problem on the accessory_booking table migration:
Schema::create('accessory_booking', function (Blueprint $table) {
$table->increments('id');
$table->integer('accessory_id')->unsigned();
$table->foreign('accessory_id')->references('id')->on('accessories');
$table->integer('booking_id')->unsigned();
$table->foreign('booking_id')->references('id')->on('bookings');
});
Please or to participate in this conversation.