无法添加外键约束-Laravel

时间:2019-06-12 07:32:22

标签: laravel

我遇到了移民问题。下表如下。

public function up()
{
    Schema::create('users_articles_likes', function (Blueprint $table) {
        // $table->increments('id');
        $table->integer('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users');
        $table->integer('article_id')->unsigned();
        $table->foreign('article_id')->references('id')->on('articles');
        $table->primary(['user_id', 'article_id']);
        $table->timestamps();
    });
}

当我尝试迁移它时。它不会推整个桌子。只需推动user_idarticle_id

这是我在终端中显示的错误。

  

SQLSTATE [HY000]:常规错误:1215无法添加外键约束   (SQL:更改表users_articles_likes添加约束   users_articles_likes_user_id_foreign外键(user_id)   引用usersid))

用户

    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });

1 个答案:

答案 0 :(得分:0)

因此问题出在数据类型错误。.在users表中,您拥有bigIncrements数据类型的Big Integer,在另一个表integer

所以尝试一下:

$table->bigInteger('user_id')->unsigned();
// or
$table->unsignedBigInteger('user_id');

确保也检查article_id

相关问题