gorm icon indicating copy to clipboard operation
gorm copied to clipboard

AutoMigrate重复创建唯一索引?

Open congjunhua opened this issue 2 years ago • 1 comments

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错误。

congjunhua avatar Sep 24 '22 12:09 congjunhua

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

github-actions[bot] avatar Sep 24 '22 12:09 github-actions[bot]

老铁,我也遇到这样的问题,你是怎么解决的

CharellKing avatar Jul 27 '23 07:07 CharellKing

老铁,我也遇到这样的问题,你是怎么解决的

unique 换成 uniqueIndex

congjunhua avatar Jul 27 '23 07:07 congjunhua

https://github.com/go-gorm/gorm/issues/6381#issuecomment-1929427042

a631807682 avatar Feb 07 '24 07:02 a631807682