反向迁移/删除自定义索引名称 laravel 迁移的外键
Reverse migration / drop foreign key for custom index name laravel migration
所以我有这个迁移
public function up()
{
Schema::table('xxx', function (Blueprint $table) {
$table->foreign('aaa','bbb') // handle identifier name too long
->references('id')->on('aaa_table')->onDelete('cascade')->onUpdate('cascade');
});
}
我想创建 down()
函数/反向迁移以删除此外键。
我在这里尝试过的:
public function down()
{
Schema::table('xxx', function (Blueprint $table) {
// implement one line of all 4 possibilities
$table->dropForeign(['aaa']);
$table->dropForeign(['bbb']);
$table->dropForeign(['aaa'], 'bbb');
$table->dropForeign(['bbb'], 'aaa');
});
}
当我 运行 回滚此迁移文件时,都出现相同的错误,
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'zzz'; check that column/key exists
数据库条件仍然干净,没有插入数据(行)。如果我在 foreign
中没有分配给第二个参数的自定义名称和短名称大小写(没有关于标识符名称太长的警告),则反向迁移 运行 会很顺利。
Alternatively, you may pass an array containing the column name that
holds the foreign key to the dropForeign method. The array will be
automatically converted using the constraint name convention used by
Laravel's schema builder:
$table->dropForeign(['user_id']);
对于我们的案例,应该是:
$table->dropForeign(['aaa','bbb']);
但这对你的情况不适用,因为:
方法foreign有两个参数,一个是列名,另一个是外键名。
在您的情况下,您使用一列作为关系,另一列用于命名!
$table->foreign('aaa','bbb')
应该是:
$table->foreign(['aaa','bbb'])
或者你可以这样写:
$table->foreign(['aaa','bbb'],'my_foreign_name')....
并在 down() 方法中
$table->dropForeign('my_foreign_name');
所以我有这个迁移
public function up()
{
Schema::table('xxx', function (Blueprint $table) {
$table->foreign('aaa','bbb') // handle identifier name too long
->references('id')->on('aaa_table')->onDelete('cascade')->onUpdate('cascade');
});
}
我想创建 down()
函数/反向迁移以删除此外键。
我在这里尝试过的:
public function down()
{
Schema::table('xxx', function (Blueprint $table) {
// implement one line of all 4 possibilities
$table->dropForeign(['aaa']);
$table->dropForeign(['bbb']);
$table->dropForeign(['aaa'], 'bbb');
$table->dropForeign(['bbb'], 'aaa');
});
}
当我 运行 回滚此迁移文件时,都出现相同的错误,
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'zzz'; check that column/key exists
数据库条件仍然干净,没有插入数据(行)。如果我在 foreign
中没有分配给第二个参数的自定义名称和短名称大小写(没有关于标识符名称太长的警告),则反向迁移 运行 会很顺利。
Alternatively, you may pass an array containing the column name that holds the foreign key to the dropForeign method. The array will be automatically converted using the constraint name convention used by Laravel's schema builder:
$table->dropForeign(['user_id']);
对于我们的案例,应该是:
$table->dropForeign(['aaa','bbb']);
但这对你的情况不适用,因为:
方法foreign有两个参数,一个是列名,另一个是外键名。
在您的情况下,您使用一列作为关系,另一列用于命名!
$table->foreign('aaa','bbb')
应该是:
$table->foreign(['aaa','bbb'])
或者你可以这样写:
$table->foreign(['aaa','bbb'],'my_foreign_name')....
并在 down() 方法中
$table->dropForeign('my_foreign_name');