简体   繁体   中英

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

I have a problem that I can not solve in any way with Laravel's migrations.

This is my users migration, data name is: 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();

        });

This is company table migration, filename is 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();
        });

This is instead a migration to set foreign keys, filename is: 2018_11_13_111338_alter_foreign_keys.php

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

When I try to run php artisan:migrate I always have this error:

    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

In last, migration, named 2018_11_13_111338_alter_foreign_keys.php I added all the other foreign keys in sequence, for all the other tables in my database.

Every suggestion is welcome, thank you.

This is code if alterForeignTable class:

<?php

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

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

You have two models and adding two foreign id. It is a many to many working. You just need add pivot table with company_id and user_id this article

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

Firstly you have to make your user_id field an index:

$table->index('user_id');

After that you can create a foreign key with an action on cascade:

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

If you want to do that with a new migration, you have to remove the index and foreign key firstly and do everything from scratch.

On down() function you have to do this and then on up() do what I've wrote above:

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

try this,hope it will work .

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