【问题标题】:Getting "Foreign key constraint is incorrectly informed" in Laravel 8在 Laravel 8 中获取“错误地通知外键约束”
【发布时间】:2021-05-22 14:23:04
【问题描述】:

我正在尝试使用 foreignId 从另一个表中获取 ID,但它不会让我这样做。

我的用户表:

public function up()
    {
        Schema::create('users', function (Blueprint $table) {
            $table->id('user_id');
            $table->string('first_name', 40);
            $table->string('last_name', 40);
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->foreignId('tier_id')->references('tier_id')->on('tiers');
            $table->rememberToken();
            $table->timestamps();
        });
    }

我的层表:

public function up()
    {
        Schema::create('tiers', function (Blueprint $table) {
            $table->id('tier_id');
            $table->string('name');
            $table->float('price');
            $table->string('max_resolution');
            $table->integer('max_users');
            $table->timestamps();
        });
    }

我做错了什么,请帮忙!

已解决,方法如下: Espresso 的评论似乎有帮助,迁移完美完成,我需要做的就是更改文件名。谢谢!

【问题讨论】:

  • 只要把你的tires迁移文件的名字改成2014_10_11_000000_create_tiers_table.php,问题就解决了
  • Espresso 的评论有帮助,我尝试了其他人,但他们没有用。谢谢!
  • 哦,抱歉,我是 SOF 的新手,谢谢!

标签: php sql laravel


【解决方案1】:

原因:您收到此错误,因为您的 users 迁移在 tires 迁移之前运行,并且当时外键 tier_id 无法从父表 @987654324 获得@

修复:只需更改您的tires 迁移文件的名称2014_10_11_000000_create_tiers_table.php,这将解决您的问题。因为现在tires 将在users 表之前运行

【讨论】:

    【解决方案2】:

    你能像这样尝试你的迁移吗 父表。

    public function up()
        {
            Schema::create('tiers', function (Blueprint $table) {
                $table->increments('tier_id');
                $table->string('name');
                $table->float('price');
                $table->string('max_resolution');
                $table->integer('max_users');
                $table->timestamps();
            });
        }
    

    子表

    public function up()
        {
            Schema::create('users', function (Blueprint $table) {
                $table->increments('users_id');
                $table->string('first_name', 40);
                $table->string('last_name', 40);
                $table->string('email')->unique();
                $table->timestamp('email_verified_at')->nullable();
                $table->string('password');
                $table->integer('tier_id')->unsigned()->nullable();
                $table->rememberToken();
                $table->timestamps();
    
                
                $table->foreign('tier_id')->references('tier_id')->on('tiers');
            });
        }
    

    【讨论】:

      【解决方案3】:

      当列类型不匹配时,通常会产生此错误。

      您的迁移看起来没有错。假设您在迁移过程中遇到错误,并且您的 tiers 迁移在 users 迁移之前,请尝试一下。

      $table->foreignId('tier_id')->constrained('tiers', 'tier_id');
      

      https://laravel.com/docs/8.x/migrations#column-method-foreignId

      如果用户迁移文件出现在层级迁移之前,则不要在users 迁移文件中创建外键,而是在tiers 迁移文件之后添加另一个迁移以添加外键约束。像这样的:

      您的users 迁移文件:

       ...
        $table->foreignId('tier_id');
       ...
      

      新的迁移文件add_tier_id_foreign_key_to_users_table

       //up
       Schema::table('users', function (Blueprint $table) {
          $table->foreign('tier_id')->references('tier_id')->on('tiers');
       });
      
       //down
       Schema::table('users', function (Blueprint $table) {
          $table->dropForeign('users_tier_id_foreign');
       });
       
       
      

      【讨论】:

        猜你喜欢
        • 2021-08-18
        • 2020-07-22
        • 2020-04-21
        • 2011-06-12
        • 2021-08-26
        • 2019-12-24
        • 2021-06-18
        • 2021-09-28
        相关资源
        最近更新 更多