【发布时间】:2020-05-25 21:01:45
【问题描述】:
我有一个名为choices 的表,我希望choice_id 是主键,但不是自动递增,因为我有一个播种机。
这是我对choices 的迁移
Schema::create('choices', function (Blueprint $table) {
$table->bigInteger('choice_id')->unsigned();
$table->bigInteger('question_id')->unsigned();
$table->string('choice');
$table->integer('value');
$table->timestamps();
});
Schema::table('choices',function (Blueprint $table){
$table->foreign('question_id')
->references('question_id')
->on('questions');
});
还有,这里是答案
Schema::create('answers', function (Blueprint $table) {
$table->bigInteger('user_id')->unsigned();
$table->bigInteger('question_id')->unsigned();
$table->bigInteger('choice_id')->unsigned();
$table->timestamps();
});
Schema::table('answers',function (Blueprint $table){
$table->foreign('question_id')
->references('question_id')
->on('questions');
$table->foreign('choice_id')
->references('choice_id')
->on('choices');
$table->foreign('user_id')
->references('user_id')
->on('schedules');
});
当我迁移时,我得到了
SQLSTATE[HY000]: General error:
1005 Can't create table
`scheduler`.`answers` (errno: 150 "Foreign key constraint is incorrectly formed")
(SQL: alter table `answers` add constraint
`answers_choice_id_foreign` foreign key (`choice_id`) references `choices` (`choice_id`))
PDOException::("SQLSTATE[HY000]:
General error: 1005 Can't create table `scheduler`.`answers` (errno: 150 "Foreign key constraint is incorrectly formed")")
【问题讨论】:
-
您没有将
unsigned添加到choices中的choice_id,因此它可能与答案中的choice_id 不匹配。 -
即使我将
$table->bigInteger('choice_id')->unsigned();放入choices我得到了同样的错误先生@aynber -
现在
choices中的choice_id是bigInteger,但answers中的choice_id只是integer。那些应该匹配。 (而不是先生) -
相同,我将其更改为
$table->bigInteger('choice_id')->unsigned();仍然得到相同的错误。我不知道为什么会这样
标签: php mysql sql laravel migration