GORM Playground Link

https://github.com/go-gorm/playground/pull/733

Description

type User struct {
    EnglishName string `json:"englishName" gorm:"uniqueIndex:idx_english_name;column:englishName;type:varchar(100);comment:英文名;"`
}

执行

DB.AutoMigrate(&User{})

的时候,初次执行正常,再次执行报错:

ALTER TABLE t_users DROP FOREIGN KEY uni_t_users_english_name
AutoMigrate for table faild. err:Error 1091 (42000): Can't DROP 'uni_t_users_english_name'; check that column/key exists

实际上是没有这个外键的,但依然会drop这个外键。

预期结果:

判断此外键是否存在,存在再去执行 Drop 方法。

Comment From: smnilu

是的,我也发生这样的报错

Comment From: eslizn

同步更新下driver版本

Comment From: haoran-mc

type PrivacyRule struct {
    gorm.Model
    Key        string `gorm:"not null;uniqueIndex:idx_privacy_rule_key;"`
    Pattern    string `gorm:"not null;"`
}

更新到最新版本 1.25.10 的 gorm 给出了错误:

time="2024-05-14T10:16:32+08:00" level=error msg="migrate failed: ERROR: constraint "uni_privacy_rules_key" of relation "privacy_rules" does not exist (SQLSTATE 42704)"

检查发现表里面有一个 constraint(不知道是哪个版本给生成了一个 idx_...... 的约束):

Indexes:
    "privacy_rules_pkey" PRIMARY KEY, btree (id)
    "idx_privacy_rule_key" UNIQUE, btree (key)
    "idx_privacy_rules_deleted_at" btree (deleted_at)
    "idx_privacy_rules_key" UNIQUE CONSTRAINT, btree (key)  // → 生成了未指定的 CONSTRAINT

目前只知道在这个 commit https://github.com/go-gorm/gorm/commit/8fb9a317756bc07dcaaa82d43613ddcf9295c1ad 只前的版本不会报错,这个 commit 之后的版本会报错。

Comment From: whatwewant

发生错误 +1

 ERROR: constraint "uni_v1_dict_uuid" of relation "v1_dict" does not exist (SQLSTATE 42704)

Comment From: siosphere

I'm facing this same issue. Any unique column I declare will AutoMigrate the first time, but then fail (with no changes) trying to drop a constraint that does not exist.

I'm using postgres/cockroackdb as the datastore

Comment From: davisfelipe

I got same issue, I partially solved it by changing the version from v1.25.9 to v1.25.5.

Comment From: qaqhy

连接mysql就更新这个驱动包 gorm.io/driver/mysql v1.5.6

连接postgres就更新这个驱动包 gorm.io/driver/postgres v1.5.7

执行下面的命令也可以 go get gorm.io/driver/mysql@latest go get gorm.io/driver/postgres@latest

Comment From: notmaster-C

实际此问题 通过查看gorm源码发现,是由于代码判断了primaryKey,没有primarykey,就会重复检测 去新增索引,目前只需要指定一个primarykey即可解决问题

type AppVersion struct {
    // Id            uint
    AppVersion    string `gorm:"type:varchar(64);not null;uniqueIndex;primaryKey" json:"appVersion"`     // 应用版本唯一id
......
    CreatedAt     time.Time `gorm:"type:datetime" json:"createdAt,omitempty"`  // 创建时间
    UpdatedAt     time.Time `gorm:"type:datetime" json:"updatedAt,omitempty"`  // 更新时间
}

func main() {
    dsn := "root:root@tcp(127.0.0.1:3306)/bigdata_uat?charset=utf8mb4&parseTime=True&loc=Local"
    db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{
    NamingStrategy: schema.NamingStrategy{
    TablePrefix: "bigdata_",
    },
    SkipDefaultTransaction: true,
    PrepareStmt:            true})
    if err != nil {
    fmt.Println(err)
    }
    db.Migrator().AutoMigrate(&AppVersion{})
}

Gorm 有 uniqueIndex 的情况下,AutoMigrate 再次执行会报错:Can't DROP 'xxx'; check that column/key exists Gorm 有 uniqueIndex 的情况下,AutoMigrate 再次执行会报错:Can't DROP 'xxx'; check that column/key exists

不管是增加id 还是指定primaryKey都可以解决此问题

我使用的gorm版本为1.25.10

Comment From: notmaster-C

I got same issue, I partially solved it by changing the version from v1.25.9 to v1.25.5.

I've also tried lowering the version, but it hasn't been effectively resolved, it just doesn't report an error, but it creates too many identical indexes. this maybe help you issue7010

Gorm 有 uniqueIndex 的情况下,AutoMigrate 再次执行会报错:Can't DROP 'xxx'; check that column/key exists

Comment From: a631807682

The error comes from updating only the gorm version but not the driver version. Refer to https://github.com/go-gorm/gorm/issues/7010#issuecomment-2129015939

Comment From: zhenxuanxin

连接mysql就更新这个驱动包 gorm.io/driver/mysql v1.5.6

连接postgres就更新这个驱动包 gorm.io/driver/postgres v1.5.7

执行下面的命令也可以 go get gorm.io/driver/mysql@latest go get gorm.io/driver/postgres@latest

实测 mysql 驱动包升级到 >= v1.5.3 即可,对比代码,发现有改动:https://github.com/go-gorm/mysql/compare/v1.5.2...v1.5.3

Comment From: keepchen

连接mysql就更新这个驱动包 gorm.io/driver/mysql v1.5.6

连接postgres就更新这个驱动包 gorm.io/driver/postgres v1.5.7

执行下面的命令也可以 go get gorm.io/driver/mysql@latest go get gorm.io/driver/postgres@latest

实测有效,感谢🙏