简体   繁体   English

无法添加外键约束, - laravel

[英]Cannot add foreign key constraint, - laravel

I am having migrations issue. 我有迁移问题。 Table is below. 表如下。

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

When I try to migrate it. 当我尝试迁移它时。 It doesn't push the whole table. 它没有推动整个表格。 Just pushes the user_id and article_id 只需按下user_idarticle_id

and this the error I am displaying in terminal. 这是我在终端显示的错误。

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table users_articles_likes add constraint users_articles_likes_user_id_foreign foreign key ( user_id ) references users ( id )) SQLSTATE [HY000]:常规错误:1215无法添加外键约束(SQL:alter table users_articles_likes add constraint users_articles_likes_user_id_foreign外键( user_id )引用usersid ))

User table 用户

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

So the problem is because of wrong data type.. In your users table you have bigIncrements which is Big Integer data type, and in the other table integer 所以问题是因为数据类型错误..在你的users表中你有bigIncrements ,它是Big Integer数据类型,而在另一个表中是integer

So try this: 试试这个:

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

make sure you check the article_id too. 确保你也查看了article_id

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM