简体   繁体   中英

Laravel 5.2 Cannot add foreign key constraint

I'm having issues with the migration registering in the migrations table and I'm getting the following error when I run php artisan migrate :

[Illuminate\Database\QueryException]                                         
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key 
constraint (SQL: alter table `surgeon_surgeon_specialty` add 
constraint `surgeon_surgeon_specialty_surgeon_id_foreign` foreign key 
(`surgeon_id`) references `surgeon` (`id`) on delete cascade)

[PDOException]                                                          
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

Here's my files currently:

Surgeons Table Migration

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateSurgeonsTable extends Migration
{
 /**
 * Run the migrations.
 *
 * @return void
 */
 public function up()
 {
    Schema::create('surgeons', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id');
        $table->string('surgeon_name', 30)->unique();
        $table->timestamps();
    });
 }

 /**
 * Reverse the migrations.
 *
 * @return void
 */
 public function down()
 {
    Schema::drop('surgeons');
 }
}

Surgeon Specialties Table Migration

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateSurgeonSpecialtiesTable extends Migration
{
 /**
 * Run the migrations.
 *
 * @return void
 */
 public function up()
 {
    Schema::create('surgeon_specialties', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->string('surgeon_specialty_name');
        $table->timestamps();
        $table->unique(['surgeon_specialty_name','user_id']);
    });
 }

 /**
 * Reverse the migrations.
 *
 * @return void
 */
 public function down()
 {
    Schema::drop('surgeon_specialties');
 }
}

Then I used the Laravel-5-Generators-Extended package to generate the

Surgeon Surgeon Specialty Table Migration

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateSurgeonSurgeonSpecialtyPivotTable extends Migration
{
 /**
 * Run the migrations.
 *
 * @return void
 */
 public function up()
 {
    Schema::create('surgeon_surgeon_specialty', function (Blueprint $table) {
        $table->integer('surgeon_id')->unsigned()->index();
        $table->foreign('surgeon_id')->references('id')->on('surgeon')->onDelete('cascade');
        $table->integer('surgeon_specialty_id')->unsigned()->index();
        $table->foreign('surgeon_specialty_id')->references('id')->on('surgeon_specialties')->onDelete('cascade');
        $table->primary(['surgeon_id', 'surgeon_specialty_id']);
    });
 }

 /**
 * Reverse the migrations.
 *
 * @return void
 */
 public function down()
 {
    // Schema::drop('surgeon_specialty_surgeon');
    Schema::drop('surgeon_surgeon_specialty');
 }
}

But changed it to this per a friends suggestion:

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateSurgeonSpecialtiesTable extends Migration
{
 /**
 * Run the migrations.
 *
 * @return void
 */
 public function up()
 {
    Schema::create('surgeon_specialties', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->string('surgeon_specialty_name');
        $table->timestamps();
        $table->unique(['surgeon_specialty_name','user_id']);
    });
 }

 /**
 * Reverse the migrations.
 *
 * @return void
 */
 public function down()
 {
    Schema::drop('surgeon_specialties');
 }
}

The table does migrate...

...however, I am still getting the error. Any help would be much appreciated.Thanks!

I had this issue on Laravel 8. I'm not sure if it's relevant to this post or not. This happens when you create the migration for the many before the one part of one-to-many relationship. In your case I guess you created migration for items before users . Simply rename your migration files according to your relationships. Your surgeon migration date should be earlier to be created before surgeon_surgeon_specialty table.

Also I suggest to declare table name of Surgeon model:

class Surgeon extends Model {
    public $table = 'surgeons';
}

This is because I see Laravel couldn't determine your table name based on your model class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM