Laravel 5.2无法添加外键约束

时间:2016-10-20 21:39:33

标签: php database laravel-5.2 pivot-table

我在migrations表中注册迁移时遇到问题,当我运行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

这是我目前的文件:

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');
 }
}

然后我使用Laravel-5-Generators-Extended包来生成

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');
 }
}

但根据朋友的建议改为:

<?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...

...但是,我仍然收到错误。任何帮助都会非常感激。谢谢!

1 个答案:

答案 0 :(得分:0)

我在 Laravel 8 上遇到过这个问题。我不确定它是否与这篇文章相关。当您在 many 关系的 one 部分之前为 one-to-many 创建迁移时,会发生这种情况。就您而言,我猜您在 items 之前为 users 创建了迁移。只需根据您的关系重命名您的迁移文件。您的 surgeon 迁移日期应该在 surgeon_surgeon_specialty 表之前创建。

另外我建议声明Surgeon模型的表名:

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

这是因为我看到 Laravel 无法根据您的模型类确定您的表名。