【发布时间】:2020-01-20 10:32:16
【问题描述】:
我有多个迁移,但我认为与这个问题相关的两个是“工作”和“会话”迁移。
工作迁移
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:修改表
session添加约束session_job_id_foreign外键 (job_id) 引用job(id))
数据库:MySQL
我不明白这里有什么问题。这种方法一直对我有用,即使在当前的 Laravel 项目中也是如此。
【问题讨论】:
-
迁移的顺序是否正确?
-
不确定你的意思。我确实先创建了会话迁移,然后是作业迁移。会不会是这个问题?
-
这就是问题所在。您尝试为不存在的表创建外键。尝试通过更改日期来重命名迁移文件。
-
好的,是的。非常感谢。