使用gorm和sqlite驱动程序时,不区分大小写的唯一列

huangapple go评论73阅读模式
英文:

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"`
}

huangapple
  • 本文由 发表于 2022年10月25日 15:41:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/74190579.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定