【问题标题】:Referencing column 'follower_id' and referenced column 'id' in foreign key constraint 'followers_ibfk_1' are incompatible. What is my mistake?在外键约束“followers_ibfk_1”中引用列“follower_id”和引用列“id”不兼容。我的错误是什么?
【发布时间】:2021-06-01 10:14:46
【问题描述】:

我正在尝试将 follower_id 连接到用户 ID,而我得到的只是这个错误。任何人都可以帮忙。这是我的代码:

 Schema::create('followers', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger("user_id");
            $table->foreignId("user_id")->constrainted()->onDelete();
            $table->unsignedBigInteger("follower_id");
            $table->foreignId("follower_id")->constrainted()->onDelete();
            $table->timestamps();
        });
    }


            $table->id();
            $table->string("username");
            $table->string("email");
           $table->string("password");
            $table->timestamps();
        });
    }

【问题讨论】:

    标签: php mysql laravel migration laravel-8


    【解决方案1】:

    要将follower_id 引用为users 表作为外键,请尝试以下操作:

    $table->unsignedBigInteger('follower_id');
    $table->foreign('follower_id')->references('id')->on('users');
    

    警告:

    指令:

    $table->foreignId("user_id")->constrainted();
    // OR
    $table->foreignId("follower_id")->constrainted();
    

    是一个快捷方式:

    $table->unsignedBigInteger('user_id');
    $table->foreign('user_id')->references('id')->on('users');
    // OR
    $table->unsignedBigInteger('follower_id');
    $table->foreign('follower_id')->references('id')->on('followers');
    

    https://laravel.com/docs/8.x/migrations#foreign-key-constraints

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-22
      • 2020-08-01
      • 2019-06-23
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 1970-01-01
      • 2021-06-12
      相关资源
      最近更新 更多