【问题标题】:In Laravel 5, migration to drop composed unique key doesn't work在 Laravel 5 中,迁移到删除组合的唯一键不起作用
【发布时间】:2016-03-29 18:03:34
【问题描述】:

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

我使用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

【问题讨论】:

  • 你试过了吗>> dropForeign().问题是 ->dropUnique 在列名上。 dropForeign() 在关系上
  • 是的,没有成功。但我想要的是撤消唯一键而不是外键。 dropUnique 也在关系中。
  • 使用SHOW CREATE TABLE <Table Name> 获取应该传递给dropUnique() 的实际约束名称。
  • 感谢@Vikas,问题已更新。
  • 根据SHOW CREATE TABLE的结果,您使用的约束名称是正确的,所以它应该可以工作。运行composer dump-autoload 或尝试在上述迁移中添加一列,以验证此迁移是否正在运行。

标签: php mysql laravel-5 artisan-migrate


【解决方案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']);
    });
}

【讨论】:

    【解决方案2】:

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

    这是你的问题

    $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 快乐编码:)

    哈基姆

    【讨论】:

    • 谢谢哈基姆。我才弄清楚发生了什么事。问题是在此之前尝试删除组合键而不删除外键和索引(KEY)。我会发布答案。
    猜你喜欢
    • 2019-08-29
    • 2019-01-22
    • 1970-01-01
    • 2021-02-08
    • 2015-01-10
    • 1970-01-01
    • 2017-07-24
    • 2015-06-03
    • 1970-01-01
    相关资源
    最近更新 更多