简体   繁体   中英

laravel migration error while adding 2 foreign key

I am using Laravel migrations to create my MySQL database, and have two tables papers and answers, and need to connect both tables using foreign keys. I have the paper_id as well as question_no as the foreign keys. but when adding foreign key I get an error. My migration for paper table and answer table

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

and this is my code for creating foreign keys,

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

The error I get through php artisan is,

Illuminate\\Database\\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table exam_paper . #sql-b88_630 (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table answers add constraint answers_paper_foreign foreign key ( paper ) references exampapers ( paper_id ))

I referred most of the other posts and already tried unsignedInteger() data type, running the table creation before foreign key creation.

What am I doing wrong in my code?

You need to add ->unsigned()->nullable()->index(); in both column (ie in paper_id and in question in exampapers table).

Try to add like below in exampapers table:

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

Now run php artisan migrate and problem fixed!

Hope this helps you!

You need to make the key in the exampapers table unsigned too:

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

Or:

$table->insignedInteger('paper_id');

Or remove the unsigned() method from the foreign key definition:

$table->integer('paper');

I have made some edits to your migration code. Hope this will work

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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