登录
首页 >  Golang >  Go问答

迁移Beego应用程序时,无法同时创建索引

来源:stackoverflow

时间:2024-03-14 09:27:24 469浏览 收藏

在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《迁移Beego应用程序时,无法同时创建索引》,聊聊,希望可以帮助到正在努力赚钱的你。

问题内容

package main

import (
    "github.com/beego/beego/v2/client/orm/migration"
)

// do not modify
type addconstrainttouser_20210928_223906 struct {
    migration.migration
}

// do not modify
func init() {
    m := &addconstrainttouser_20210928_223906{}
    m.created = "20210928_223906"

    migration.register("addconstrainttouser_20210928_223906", m)
}

// run the migrations
func (m *addconstrainttouser_20210928_223906) up() {
    // use m.sql("create table ...") to make schema update
    m.sql("end;" +
        "create unique index  concurrently if not exists uniq_email on users (email) where status_id=1;" +
        "create unique index  concurrently if not exists uniq_name on users (name) where status_id=1;" +
        "begin;")
}

// reverse the migrations
func (m *addconstrainttouser_20210928_223906) down() {
    // use m.sql("drop table ...") to reverse schema update
    m.sql("end;" +
        "drop index concurrently if exists uniq_email;" +
        "drop index concurrently if exists  uniq_name;" +
        "begin;")
}

这是我的迁移文件。在运行 bee migrate 时,它会抛出一个错误:

2021/09/29 04:26:03 INFO     ▶ 0001 Using 'postgres' as 'driver'
2021/09/29 04:26:03 INFO     ▶ 0002 Using '/Users/aashishpassrija/go/src/users/database/migrations' as 'dir'
2021/09/29 04:26:03 INFO     ▶ 0003 Running all outstanding migrations
2021/09/29 04:26:08 INFO     ▶ 0004 |> 2021/09/29 04:26:06.018 [I]  start upgrade AddConstraintToUser_20210928_223906
2021/09/29 04:26:08 INFO     ▶ 0005 |> 2021/09/29 04:26:06.018 [I]  exec sql: END;CREATE UNIQUE INDEX  CONCURRENTLY IF NOT EXISTS uniq_email ON users (email) WHERE status_id=1;CREATE UNIQUE INDEX  CONCURRENTLY IF NOT EXISTS uniq_name ON users (name) WHERE status_id=1;BEGIN;
2021/09/29 04:26:08 INFO     ▶ 0006 |> 2021/09/29 04:26:06.018 [E]  execute error: pq: CREATE INDEX CONCURRENTLY cannot run inside a transaction block
2021/09/29 04:26:08 ERROR    ▶ 0007 Could not run migration binary: exit status 2

现在我知道索引不能在事务内同时创建。但考虑到我的数据库很大。我该如何实现这一目标? beego 是否有相当于 disable_ddl_transaction 的功能?


正确答案


如果您向服务器发送多个用分号分隔的语句,它们将全部在单个事务中运行。但是您无法在多语句事务中同时运行 CREATE INDEX

启用自动提交(如果禁用),并单独运行每个 CREATE INDEX

今天关于《迁移Beego应用程序时,无法同时创建索引》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>