0

尝试进行迁移以删除组合的唯一密钥失败且没有错误。

我已经创建了一个迁移php artisan make:migration并编辑了代码。所以我有这个

public function up()
{
    Schema::table('ques_trilha_itens', function (Blueprint $table) {
        $table->dropUnique('trilha_itens_trilha_id_questao_id_unique');
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('ques_trilha_itens', function (Blueprint $table) {
        $table->unique(['trilha_id', 'questao_id']);
    });
}

该字符串'trilha_itens_trilha_id_questao_id_unique'是在 MySQL 中显示为组合唯一键的字符串。所以我认为要使用该字符串来删除两个组合键。

但是在运行时php artisan migrate,什么都没有发生,没有错误消息,并且没有执行迁移。

我尝试将字符串替换为dropUnique表的名称作为第一个术语 ( 'ques_trilha_itens_trilha_id_questao_id_unique') 而什么都没有。

我错过了什么吗?

更新

MySQL 命令SHOW CREATE TABLE ques_trilha_itens给出:

CREATE TABLE `ques_trilha_itens` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `trilha_id` int(10) unsigned NOT NULL,
  `questao_id` int(10) unsigned NOT NULL,
  `primeiro_item` tinyint(1) NOT NULL,
  `item_principal_id` int(10) unsigned DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
  `chave` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `trilha_itens_trilha_id_questao_id_unique` (`trilha_id`,`questao_id`),
  UNIQUE KEY `ques_trilha_itens_chave_unique` (`chave`),
  KEY `trilha_itens_questao_id_foreign` (`questao_id`),
  KEY `ques_trilha_itens_item_principal_id_foreign` (`item_principal_id`),
  CONSTRAINT `ques_trilha_itens_item_principal_id_foreign` FOREIGN KEY (`item_principal_id`) REFERENCES `ques_trilha_itens` (`id`) ON DELETE SET NULL,
  CONSTRAINT `trilha_itens_trilha_id_foreign` FOREIGN KEY (`trilha_id`) REFERENCES `ques_trilhas` (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
4

2 回答 2

1

好吧,通过反复试验,我弄清楚了我错过了什么。

问题是 MySQL 不希望您在自己删除这些外键之前尝试删除外部引用的组合键。

CONSTRAINT `ques_trilha_itens_item_principal_id_foreign` FOREIGN KEY (`item_principal_id`) REFERENCES `ques_trilha_itens` (`id`) ON DELETE SET NULL,
CONSTRAINT `trilha_itens_trilha_id_foreign` FOREIGN KEY (`trilha_id`) REFERENCES `ques_trilhas` (`id`)

像这样,我们KEY之前也必须删除索引 ( )。

KEY `trilha_itens_questao_id_foreign` (`questao_id`)

只有在那之后,我才能删除组合键['questao_id', 'trilha_id']

所以最后我的迁移是如此

/**
 * Run the migrations.
 *
 * @return void
 */

public function up()
    {
        Schema::table('ques_trilha_itens', function (Blueprint $table) {
            // Remove chaves estrangeiras, índices e chaves não estrangeiras
            $table->dropForeign('trilha_itens_questao_id_foreign');
            $table->dropForeign('trilha_itens_trilha_id_foreign');
            $table->dropIndex('trilha_itens_questao_id_foreign');
            $table->dropUnique('trilha_itens_trilha_id_questao_id_unique');

            // Refaz relações, agora sem a chave dupla
            $table->foreign('questao_id')->references('id')->on('ques_questoes');
            $table->foreign('trilha_id')->references('id')->on('ques_trilhas');
        });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::table('ques_trilha_itens', function (Blueprint $table) {
        $table->unique(['questao_id', 'trilha_id']);
    });
}
于 2016-03-30T11:02:28.810 回答
0

我试图弄清楚发生了什么然后它来找我。

这是你的问题

$table->dropUnique('trilha_itens_trilha_id_questao_id_unique'); <<< here be the problem

你的问题是你试图同时做太多的事情。

删除唯一键。您必须使用适当的命名法。

table_name_column_name_unique 1) table_name _ 2) column_name _ 3) 唯一

所以你必须运行你的“dropUnique()”两次。

$table->dropUnique('trilha_itens_trilha_id_unique'); //for trilha_id
$table->dropUnique('trilha_itens_questao_id_unique'); //for questao_id

试试看,让我知道结果如何。当然,请确保您运行 composer dump-autoload 快乐编码 :)

哈基姆

于 2016-03-30T00:52:32.337 回答