go-gorm / playground

GORM Playground (Please Create PR for this project to report issues)
MIT License
88 stars 666 forks source link

add uniqueIndex for AutoMigrate #733

Open ocean-gao opened 2 months ago

ocean-gao commented 2 months ago

Explain your user case and expected results

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 方法。