gorm
gorm copied to clipboard
AutoMigrate重复创建唯一索引?
Your Question
type User struct {
gorm.Model
PublicID uint `gorm:"type:char(10);not null;unique"`
}
db.AutoMigrate(&User{})
为什么这样子写法每次重新运行程序时MySQL
都会增加一个键和索引呢?
-- key
public_id
public_id_1
public_id_2
public_id_3
...
-- unique
public_id
public_id_1
public_id_2
public_id_3
...
直到报Error 1069: Too many keys specified; max 64 keys allowed
错误。
The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question
template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨
老铁,我也遇到这样的问题,你是怎么解决的
老铁,我也遇到这样的问题,你是怎么解决的
将 unique
换成 uniqueIndex
。
https://github.com/go-gorm/gorm/issues/6381#issuecomment-1929427042