首页 文章

如何为Gorm指定具有多列唯一索引的结构?

提问于
浏览
0

如何定义我的 struct 以指定Gorm in Go的多列唯一索引?

如:

type Something struct {
    gorm.Model
    First  string `sql:"unique_index:unique_index_with_second"`
    Second string `sql:"unique_index:unique_index_with_first"`
}

1 回答

  • 0

    您是否尝试创建一个表,以便First和Second的组合是唯一的?

    这应该工作:

    type Something struct {
        ID uint
        gorm.Model
        First  string `gorm:"primary_key"`
        Second string `gorm:"primary_key"`
    }
    

相关问题