如何在Gorm中指定唯一的索引?

发布于 2025-02-13 20:19:42 字数 298 浏览 0 评论 0原文

type ABC struct {
ID uint
 Abc int `gorm:"unidqueIndex;
Bcd string
}

我想要字段abcbcd是唯一在一起

{abc:1,bcd:“ hello”} {abc:1,bcd:“ hi”} < /code>应该有效,但

{ABC:1,BCD:“ Hello”} {ABC:1,BCD:“ Hello”}应该无效

type ABC struct {
ID uint
 Abc int `gorm:"unidqueIndex;
Bcd string
}

I want Field Abc and Bcd to be unique together

{Abc: 1, Bcd: "hello"} {Abc: 1, Bcd: "hi"} should be valid but

{Abc: 1, Bcd: "hello"} {Abc: 1, Bcd: "hello"} should be invalid

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦过后 2025-02-20 20:19:43

给两个字段都有相同的索引名称。

type ABC struct {
 ID uint
 Abc int `gorm:"uniqueIndex:abc_bcd_uniq"`
 Bcd string `gorm:"uniqueIndex:abc_bcd_uniq"`
}

参见 gorm doc中的复合索引

Give both fields the same index name.

type ABC struct {
 ID uint
 Abc int `gorm:"uniqueIndex:abc_bcd_uniq"`
 Bcd string `gorm:"uniqueIndex:abc_bcd_uniq"`
}

See composite indexes in the GORM docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文