Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
ocean-gao opened this issue May 9, 2024 · 7 comments
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@ocean-gao
Copy link

GORM Playground Link

go-gorm/playground#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 方法。

@github-actions github-actions bot added the type:with reproduction steps with reproduction steps label May 9, 2024
@smnilu
Copy link

smnilu commented May 10, 2024

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

@eslizn
Copy link

eslizn commented May 11, 2024

同步更新下driver版本

@haoran-mc
Copy link

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 8fb9a31 只前的版本不会报错,这个 commit 之后的版本会报错。

@whatwewant
Copy link

发生错误 +1

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

@siosphere
Copy link

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

@davisfelipe
Copy link

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

@qaqhy
Copy link

qaqhy commented May 24, 2024

连接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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:with reproduction steps with reproduction steps
Projects
None yet
Development

No branches or pull requests

9 participants