英文:
Case insensitive unique column when using gorm with sqlite driver
问题
在我的 Golang 应用程序中,我在一个结构体中使用了一个 Name 列。
我想以原始形式保存文本,例如 "User1",但 "user1" 被视为重复。在 gorm 和 sqlite 驱动中是否可以实现这个功能?
英文:
In my Golang application, I am using a Name column in one of my structs.
I want to save the text in its original form, so for example "User1", but "user1" would be considered to be duplicate. Is this possible with gorm and sqlite driver?
答案1
得分: 1
手动将列的数据类型覆盖为"text collate nocase"对我有用,像这样:
type User struct {
ID int
Name string `gorm:"index:,unique;type:text collate nocase"`
}
英文:
Manually overriding the column's datatype to 'text collate nocase' worked for me, like so:
type User struct {
ID int
Name string `gorm:"index:,unique;type:text collate nocase"`
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论