SQLSTATE[HY000]:一般错误:1215无法添加外键约束Laravel 5.4



我有一个问题,用Laravel的迁移无法以任何方式解决。

这是我的用户迁移,数据名称为:2014_12_10_000000_create_users_table.php

Schema::create('users', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->unsignedInteger('user_parent_id')->nullable();
            $table->unsignedInteger('company_id')->nullable();
            $table->string('name');
            $table->string('email')->unique();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

这是公司表迁移,文件名为2018_06_01_080858_create_company_table.php

Schema::create('company', function (Blueprint $table) {
            $table->engine = 'InnoDB';
            $table->increments('id');
            $table->unsignedInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users');
             ....
            $table->timestamps();
        });

这是一个设置外键的迁移,文件名为:2018_11_13_111338_alter_forging_keys.php

Schema::table('users', function($table) {
          $table->foreign('user_parent_id')->references('id')->on('users');
          $table->foreign('company_id')->references('id')->on('company');
      });

当我尝试运行php-artisan:migrate时,我总是会出现以下错误:

    In Connection.php line 647:                                                                               
      SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `users` add constraint `users_company_id_foreign` foreign key (`company_id`) references `company` (`id`))                 
In PDOStatement.php line 144:                                                                          
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint      
In PDOStatement.php line 142:                                                                          
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint

在最后的迁移中,名为2018_11_13_111338_alter_forging_keys.php,我为数据库中的所有其他表按顺序添加了所有其他外键。

欢迎提出任何建议,谢谢。

这是alterForeignTable类的代码:

<?php
use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;
class AlterForeignTable2 extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {

      Schema::table('users', function($table) {
          $table->foreign('user_parent_id')->references('id')->on('users');
          $table->foreign('company_id')->references('id')->on('company');
      });

      Schema::table('otherstable', function($table) {
          $table->foreign('user_id')->references('id')->on('users');
      });
    }
}

您有两个模型,并添加了两个外部id。这是一个多对多的工作。您只需要在本文中添加带有company_id和user_id的透视表

https://laraveldaily.com/pivot-tables-and-many-to-many-relationships/

首先,您必须使user_id字段成为索引:

$table->index('user_id');

之后,您可以创建一个具有级联操作的外键:

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

如果你想在新的迁移中做到这一点,你必须首先删除索引和外键,然后从头开始。

在down((函数上,你必须这样做,然后在up((上做我上面写的:

$table->dropForeign('user_parent_id');
$table->dropIndex('lists_user_id_index');
$table->dropColumn('user_id');

试试这个,希望它能起作用。

最新更新