【发布时间】:2016-12-16 11:37:55
【问题描述】:
我有一个表(名为j_stones),由 5 个字段组成:
id(primary key)
j_stones_type(foreign-key)
shape
size
carat
我想让每一行都独一无二,所以我创建了一个迁移来做到这一点:
public function up()
{
Schema::table('j_stones', function (Blueprint $table) {
$table->unique(['j_stone_types_id','shape','size','carat']);
});
}
这很好用,但是当我尝试回滚时,我收到以下错误:
1[照亮\数据库\查询异常]
SQLSTATE[HY000]:一般错误:1553 无法删除索引
'j_stones_j_stone_types_id_shape_size_carat_unique':需要 外键约束(SQL:alter table
j_stonesdrop indexj_stones_j_stone_types_id_shape_size_carat_unique)[PDOException] SQLSTATE[HY000]:一般错误:1553 无法删除 索引“j_stones_j_stone_types_id_shape_size_carat_unique”:需要 外键约束'
这是我的回滚代码:
public function down()
{
Schema::table('j_stones', function (Blueprint $table) {
$table->dropUnique(['j_stone_types_id','shape','size','carat']);
});
}
我尝试像这样禁用外键约束:
public function down()
{
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
Schema::table('j_stones', function (Blueprint $table) {
$table->dropUnique(['j_stone_types_id','shape','size','carat']);
});
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
}
还有这样的:
public function down()
{
Schema::disableForeignKeyConstraints();
Schema::table('j_stones', function (Blueprint $table) {
$table->dropUnique(['j_stone_types_id','shape','size','carat']);
});
Schema::enableForeignKeyConstraints();
}
但在回滚时仍然不断发生错误。
我将 MySql 与 InnoDb 一起使用。
请指教。
编辑:
我使用以下 hack 进行了回滚,但仍在寻找正确的解决方案:
public function down()
{
Schema::table('j_stones', function (Blueprint $table) {
$table->dropForeign(['j_stone_types_id']);
$table->dropUnique(['j_stone_types_id','shape','size','carat']);
});
}
【问题讨论】:
标签: php mysql laravel laravel-5