Php 如何在laravel 5.2迁移中删除包含外键的复合键
我有一个表(名为Php 如何在laravel 5.2迁移中删除包含外键的复合键,php,mysql,laravel,laravel-5,Php,Mysql,Laravel,Laravel 5,我有一个表(名为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
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_type_id_shape_size_carat_unique”:在
外键约束(SQL:alter tablej_stones
drop index
j_石_石_石_石_石_石_石_石_石_石_石_石_石_石_石_石_石_石_石_石_石_形状_石_石_石_石_石_石
[PDOException]SQLSTATE[HY000]:一般错误:1553无法删除
索引'j_stones\u j_stone\u类型\u id\u形状\u大小\u克拉\u唯一]:中需要
外键约束'
这是我的回滚代码:
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();
}
但在回滚时仍然会发生错误。
我在InnoDb中使用MySql
请给我一些建议
编辑:
我使用下面的方法实现了回滚,但仍在寻找合适的解决方案:
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']);
});
}
我刚刚遇到了同样的问题,这对我很有效:
public function down()
{
Schema::table('j_stones', function (Blueprint $table) {
$table->index('j_stone_types_id']);
$table->dropUnique(['j_stone_types_id','shape','size','carat']);
});
}
因此,首先在列上添加外键约束所需的新索引,然后可以删除复合键。我也面临同样的问题。我解决这个问题的方法是在迁移的down()函数中执行以下操作:
放下外键
删除唯一的复合索引
再次创建外键
例如
这样,每次我回滚迁移时,我的DB和外键都会返回到以前的状态。这里是解决方法
public function down()
{
Schema::table('j_stones', function (Blueprint $table) {
$table->dropForeign(/* Foreign key name */);
});
Schema::table('j_stones', function (Blueprint $table) {
$table->dropUnique(/* Array of fields for the composite index */);
});
Schema::table('j_stones', function (Blueprint $table) {
/* Creating back the foreign key here, just like the way it was before running the migration */
});
}
public function up() {
Schema::table('contact_tags', function (Blueprint $table) {
$table->unique(['tag_id', 'contact_id'], 'tag_contact_unique'); //so that contact cannot have the same tag multiple times
});
}
public function down() {
Schema::table('contact_tags', function (Blueprint $table) {
//THE FOLLOWING 2 COMMANDS ARE THE WORKAROUND
//Although this feels weird, we first need to add the missing indexes:
$table->index('tag_id', 'tag_id_foreign');
$table->index('contact_id', 'contact_id_foreign');
//Now proceed with the main part of this "down" command to drop the unique index:
$table->dropUnique('tag_contact_unique');
});
}