簡體   English   中英

無法添加外鍵約束, - laravel

[英]Cannot add foreign key constraint, - 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:alter table users_articles_likes add constraint 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();
    });

所以問題是因為數據類型錯誤..在你的users表中你有bigIncrements ,它是Big Integer數據類型,而在另一個表中是integer

試試這個:

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

確保你也查看了article_id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM