laravel6: 如何通过迁移更改外键的引用
laravel6: how to change references of a foreign key with migration
我尝试通过 dropforeign 更改外键引用 key.I 认为一切正常,但我收到此错误:
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP
FOREIGN KEY teacher_schedule_calendars_product_id_foreign; check
that it exists (SQL: alter table teacher_schedule_calendars drop
foreign key teacher_schedule_calendars_product_id_foreign)
我该如何解决?
我的迁移代码是:
Schema::table('teacher_schedule_calendars', function (Blueprint $table) {
$table->dropForeign(['product_id']);
$table->foreign('product_id')->references('id') ->on('courses')->onDelete('cascade');
});
首先,我删除了外来的,然后添加了引用。但是没有用。
我假设您在之前的迁移中在 product_id
列上创建了一个外键,您只是想更新它或用另一个 table 的外键替换它。在这种情况下,由于迁移的工作方式以及各个数据库语法如何转换命令,您尝试执行的操作可能会出现问题。
要缓解此问题,您可以将命令分成两个 Schema::table($table)
块:
Schema::table('teacher_schedule_calendars', function (Blueprint $table) {
$table->dropForeign(['product_id']));
});
Schema::table('teacher_schedule_calendars', function (Blueprint $table) {
$table->foreign('product_id')->references('id')->on('courses')->onDelete('cascade');
});
我尝试通过 dropforeign 更改外键引用 key.I 认为一切正常,但我收到此错误:
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP FOREIGN KEY teacher_schedule_calendars_product_id_foreign; check that it exists (SQL: alter table teacher_schedule_calendars drop foreign key teacher_schedule_calendars_product_id_foreign)
我该如何解决?
我的迁移代码是:
Schema::table('teacher_schedule_calendars', function (Blueprint $table) {
$table->dropForeign(['product_id']);
$table->foreign('product_id')->references('id') ->on('courses')->onDelete('cascade');
});
首先,我删除了外来的,然后添加了引用。但是没有用。
我假设您在之前的迁移中在 product_id
列上创建了一个外键,您只是想更新它或用另一个 table 的外键替换它。在这种情况下,由于迁移的工作方式以及各个数据库语法如何转换命令,您尝试执行的操作可能会出现问题。
要缓解此问题,您可以将命令分成两个 Schema::table($table)
块:
Schema::table('teacher_schedule_calendars', function (Blueprint $table) {
$table->dropForeign(['product_id']));
});
Schema::table('teacher_schedule_calendars', function (Blueprint $table) {
$table->foreign('product_id')->references('id')->on('courses')->onDelete('cascade');
});