我有多个迁移,但是我认为与该问题相关的两个迁移是“工作”和“会话”迁移。
工作迁移
Schema::create('job', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
});
会话迁移:
Schema::create('session', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('group_id');
$table->unsignedBigInteger('job_id');
$table->boolean('verified')->default(0);
$table->date('date');
$table->time('start_time');
$table->time('end_time');
$table->string('session_type');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('group_id')->references('id')->on('group');
$table->foreign('job_id')->references('id')->on('job');
});
现在我进行迁移时遇到的错误是:
SQLSTATE [HY000]:常规错误:1215无法添加外键约束
(SQL:alter table
session
添加约束session_job_id_foreign
外键(
job_id
)引用job
(id
))数据库:MySQL
我不明白这里是什么问题。即使在当前的Laravel项目中,这种方法也一直对我有用。
最佳答案
您必须确保作业迁移在会话迁移之前进行