您的位置 首页 MySql

php – 如果索引存在于Laravel迁移中,如何检查索引?

在准备迁移时,尝试检查表上是否存在唯一索引,如何实现?

Schema::table('persons',function (Blueprint $table) {
    if ($table->hasIndex('persons_body_unique')) {
        $table->dropUnique('persons_body_unique');
    }
})

看起来像上面的东西. (显然,hasIndex()不存在)
最佳答案
使用Laravel使用的“doctrine-dbal”是更好的解决方案:

Schema::table('persons',function (Blueprint $table) {
    $sm = Schema::getConnection()->getDoctrineSchemaManager();
    $indexesFound = $sm->listTableIndexes('persons');

    if(array_key_exists("persons_body_unique",$indexesFound))
        $table->dropUnique("persons_body_unique");
})

关于作者: dawei

【声明】:金华站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

热门文章