laravel - 为什么 laravel 显示“外键约束格式不正确”?
问题描述
我正在尝试进行 laravel 迁移并在两个表之间建立外键约束。这是我的一些代码:
创建用户教育表
class CreateUserEducationTable extends Migration
{
public function up()
{
Schema::create('user_education', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')
->references('id')
->on('users')
->onUpdate('CASCADE')
->onDelete('CASCADE');
$table->unsignedBigInteger('institution_id');
$table->foreign('institution_id')
->references('id')
->on('education_institutions')
->onUpdate('CASCADE')
->onDelete('CASCADE');
$table->string('degree')->nullable();
$table->string('grade')->nullable();
$table->year('start_year')->nullable();
$table->year("end_year")->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::table('user_education', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropForeign(['institution_id']);
});
Schema::dropIfExists('user_education');
}
}
创建教育机构表
class CreateEducationInstitutionsTable extends Migration
{
public function up()
{
Schema::create('education_institutions', function (Blueprint $table) {
$table->id();
$table->string('institution_name');
$table->string('country');
$table->string('city');
$table->string('logo')->nullable();
$table->text('description')->nullable();
$table->timestamps();
});
}
public function down()
{
Schema::dropIfExists('education_institutions');
}
}
在迁移 ```user_education_table`` 时会抛出以下错误。
这里有什么意义?
解决方案
您必须在创建education_institutions
表之前创建user_education
表
推荐阅读
- cuda - 修改基本示例 VECADD 以使用共享内存
- typescript - 使用 Typescript 时解析器出现 Apollo GraphQL Schema Stitching 问题
- java - 如何绘制多边形,然后按比例调整大小并将其移动到显示区域的中心?
- button - Flutter:设置 RatingBar 值时启用按钮
- python - 为什么我的 .current 没有为组合框设置默认值,即使我使用了一个类来初始化?
- javascript - 如何使用 Cypress 在 Sharepoint 上测试 .aspx 页面
- algorithm - 如何在不同的运行时间对给定记录进行排序 - O(n) 和 O(nlogk),其中 k 是具有键值对的序列 n 的不同值?
- javascript - 无法使用 laravel 5.5 中的 ConsoleTV 图表包从数据库中填充数据集
- android-studio - 运行我的第一个颤振项目时出错
- python - Tensorflow Object Detection - mAP 稳定,但 Total Loss 正在下降。继续训练?