简体   繁体   English

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

[英]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. 我有一个Laravel迁移无法解决的问题。

This is my users migration, data name is: 2014_12_10_000000_create_users_table.php 这是我的用户迁移,数据名称为: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 这是公司表迁移,文件名是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 相反,这是设置外键的迁移,文件名是: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: 当我尝试运行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

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. 最后,进行迁移,命名为2018_11_13_111338_alter_foreign_keys.php,我为数据库中的所有其他表依次添加了所有其他外键。

Every suggestion is welcome, thank you. 任何建议都是值得欢迎的,谢谢。

This is code if alterForeignTable class: 这是alterForeignTable类的代码:

<?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. 您有两个模型,并添加了两个外部ID。 It is a many to many working. 这是许多工作。 You just need add pivot table with company_id and user_id this article 您只需要添加带有company_id和user_id的数据透视表即可

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

Firstly you have to make your user_id field an index: 首先,您必须将user_id字段设为索引:

$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: 在down()函数中,您必须执行此操作,然后在up()中,执行我上面写的内容:

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

try this,hope it will work . 试试这个,希望它能起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Laravel 6:SQLSTATE [HY000]:常规错误:1215无法添加外键约束 - Laravel 6: SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint SQLSTATE[HY000]:一般错误:1215 无法添加外键约束 [Laravel 7.0] - SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint [Laravel 7.0] laravel migration SQLSTATE [HY000]:常规错误:1215无法添加外键约束 - laravel migration SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint Laravel 5.0 [PDOException] SQLSTATE [HY000]:常规错误:1215无法添加外键约束 - Laravel 5.0 [PDOException] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint SQLSTATE[HY000]:一般错误:1215 无法添加外键约束(Laravel) - SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (Laravel) Laravel:如何解决“SQLSTATE [HY000]:一般错误:1215 无法添加外键约束” - Laravel: How to solve “SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint” SQLSTATE [HY000]:一般错误:1215 无法在 laravel 8 中添加外键约束 - SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint in laravel 8 迁移时出现“SQLSTATE [HY000]:一般错误:1215 无法添加外键约束”错误 - getting 'SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint' error with migration SQLSTATE[HY000]:一般错误:1215 无法添加外键约束错误 - SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint Error 问题 SQLSTATE[HY000]: 一般错误:1215 无法添加外键约束 - Problem SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM