[英]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.我正在使用 Laravel 迁移来创建我的 MySQL 数据库,并且有两个表论文和答案,并且需要使用外键连接两个表。 I have the
paper_id
as well as question_no
as the foreign keys.我有
paper_id
以及question_no
作为外键。 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,我通过 php artisan 得到的错误是,
Illuminate\\Database\\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table
exam_paper
.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 tableanswers
add constraintanswers_paper_foreign
foreign key (paper
) referencesexampapers
(paper_id
))#sql-b88_630
(errno: 150 "外键约束#sql-b88_630
不正确") (SQL: alter tableanswers
添加约束answers_paper_foreign
外键 (paper
) 参考exampapers
(paper_id
))
I referred most of the other posts and already tried unsignedInteger()
data type, running the table creation before foreign key creation.我参考了其他大部分帖子,并且已经尝试过
unsignedInteger()
数据类型,在创建外键之前运行表创建。
What am I doing wrong in my code?我的代码做错了什么?
You need to add ->unsigned()->nullable()->index();
您需要添加
->unsigned()->nullable()->index();
in both column (ie in paper_id
and in question
in exampapers
table).在两列中(即在
paper_id
和exampapers
表中的question
)。
Try to add like below in exampapers
table:尝试在
exampapers
表中添加如下exampapers
:
$table->integer('paper_id')->unsigned()->nullable()->index();
$table->integer('question_no')->unsigned()->nullable()->index();
Now run php artisan migrate
and problem fixed!现在运行
php artisan migrate
并修复问题!
Hope this helps you!希望这对你有帮助!
You need to make the key in the exampapers
table unsigned too:您还需要使
exampapers
表中的密钥未签名:
$table->integer('paper_id')->unsigned();
Or:或者:
$table->insignedInteger('paper_id');
Or remove the unsigned()
method from the foreign key definition:或者从外键定义中删除
unsigned()
方法:
$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');
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.