复合主键

    通过将多个字段设为主键,以创建复合主键,例如:

    type Product struct {
      ID           string `gorm:"primaryKey"`
      LanguageCode string `gorm:"primaryKey"`
      Code         string
      Name         string
    }

    注意:默认情况下,整型 PrioritizedPrimaryField 启用了 AutoIncrement,要禁用它,您需要为整型字段关闭 autoIncrement:

    type Product struct {
      CategoryID uint64 `gorm:"primaryKey;autoIncrement:false"`
      TypeID     uint64 `gorm:"primaryKey;autoIncrement:false"`
    }
    最后编辑: Simon  文档更新时间: 2021-04-06 14:54   作者:Simon