添加2个外键时laravel迁移错误

时间:2018-02-22 06:12:04

标签: mysql laravel foreign-keys migration

我正在使用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工匠得到的错误是,

  

Illuminate \ Database \ QueryException:SQLSTATE [HY000]:常规错误:   1005       无法创建表exam_paper#sql-b88_630(错误:150“外键       约束形成错误“)(SQL:alter table answers add       约束answers_paper_foreign       外键(paper)引用exampaperspaper_id))

我引用了大多数其他帖子并且已经尝试了unsignedInteger()数据类型,在创建外键之前运行表创建。

我的代码中出错了什么?

3 个答案:

答案 0 :(得分:1)

您需要在两个列中添加->unsigned()->nullable()->index();(即paper_idquestion exampapers表中的exampapers

尝试在$table->integer('paper_id')->unsigned()->nullable()->index(); $table->integer('question_no')->unsigned()->nullable()->index(); 表中添加如下内容:

php artisan migrate

现在运行w=function(u,l){ a=2:l -0.5772-log(u)+u+sum(u^(a)*rep(c(-1,1),length=l-1)/(a)/factorial(a)) } transform(Wu_QC,new=Vectorize(w)(u,170)) u Wu_table new 1 1.0e-15 3.39616e+01 3.396158e+01 2 4.1e-14 3.02480e+01 3.024800e+01 3 9.9e-13 2.70639e+01 2.706387e+01 4 7.0e-12 2.51079e+01 2.510791e+01 5 3.7e-11 2.34429e+01 2.344290e+01 6 2.3e-10 2.16157e+01 2.161574e+01 7 6.8e-09 1.82291e+01 1.822914e+01 8 5.7e-08 1.61030e+01 1.610301e+01 9 8.4e-07 1.34126e+01 1.341266e+01 10 6.3e-06 1.13978e+01 1.139777e+01 11 3.1e-05 9.80430e+00 9.804354e+00 12 7.4e-04 6.63240e+00 6.632400e+00 13 5.1e-03 4.70640e+00 4.706408e+00 14 2.9e-02 2.99200e+00 2.992051e+00 15 8.7e-01 2.74200e-01 2.741930e-01 16 4.6e+00 1.84100e-03 1.856671e-03 17 9.9e+00 4.63700e-06 2.030179e-05 并修复问题!

希望这能帮到你!

答案 1 :(得分:0)

您还需要在exampapers表格中设置无符号密钥:

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

或者:

$table->insignedInteger('paper_id');

或者从外键定义中删除unsigned()方法:

$table->integer('paper');

答案 2 :(得分:0)

我对您的迁移代码进行了一些修改。希望这会起作用

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