簡體   English   中英

添加 2 個外鍵時 Laravel 遷移錯誤

[英]laravel migration error while adding 2 foreign key

我正在使用 Laravel 遷移來創建我的 MySQL 數據庫,並且有兩個表論文和答案,並且需要使用外鍵連接兩個表。 我有paper_id以及question_no作為外鍵。 但是在添加外鍵時出現錯誤。 我的紙表和答案表的遷移

Schema::create('exampapers', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->integer('paper_id');
        $table->integer('question_no');
        $table->text('question');
        $table->string('answer1');
        $table->string('answer2');
        $table->string('answer3');
        $table->string('answer4');
        $table->integer('answerC');
        $table->string('knowarea');
        $table->timestamps();
        $table->index(['paper_id','question_no']);

});

Schema::create('answers', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->integer('paper')->unsigned();
        $table->integer('question')->unsigned();
        $table->integer('answers');
        $table->timestamps();
});

這是我創建外鍵的代碼,

Schema::table('answers',function($table){
        $table->foreign('paper')->references('paper_id')->on('exampapers');
        $table->foreign('question')->references('question_no')->on('exampapers');
});

我通過 php artisan 得到的錯誤是,

Illuminate\\Database\\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table exam_paper #sql-b88_630 (errno: 150 "外鍵約束#sql-b88_630不正確") (SQL: alter table answers添加約束answers_paper_foreign外鍵 ( paper ) 參考exampapers ( paper_id ))

我參考了其他大部分帖子,並且已經嘗試過unsignedInteger()數據類型,在創建外鍵之前運行表創建。

我的代碼做錯了什么?

您需要添加->unsigned()->nullable()->index(); 在兩列中(即在paper_idexampapers表中的question )。

嘗試在exampapers表中添加如下exampapers

$table->integer('paper_id')->unsigned()->nullable()->index();
$table->integer('question_no')->unsigned()->nullable()->index();

現在運行php artisan migrate並修復問題!

希望這對你有幫助!

您還需要使exampapers表中的密鑰未簽名:

$table->integer('paper_id')->unsigned();

或者:

$table->insignedInteger('paper_id');

或者從外鍵定義中刪除unsigned()方法:

$table->integer('paper');

我對您的遷移代碼進行了一些編輯。 希望這會奏效

Schema::create('exampapers', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->integer('paper_id')->unsigned()->index();
        $table->integer('question_no')->unsigned()->index();
        $table->text('question');
        $table->string('answer1');
        $table->string('answer2');
        $table->string('answer3');
        $table->string('answer4');
        $table->integer('answerC');
        $table->string('knowarea');
        $table->timestamps();
        //$table->index(['paper_id','question_no']);

    });

Schema::create('answers', function (Blueprint $table) {
        $table->engine = 'InnoDB';
        $table->increments('id');
        $table->integer('paper')->unsigned()->index();
        $table->integer('question')->unsigned()->index();
        $table->integer('answers');
        $table->timestamps();
    });


Schema::table('answers',function($table){
        $table->foreign('paper')->references('paper_id')->on('exampapers');
        $table->foreign('question')->references('question_no')->on('exampapers');
    });

暫無
暫無

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

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