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

    1. type Product struct {
    2. ID string `gorm:"primaryKey"`
    3. LanguageCode string `gorm:"primaryKey"`
    4. Code string
    5. Name string
    6. }

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

    1. type Product struct {
    2. CategoryID uint64 `gorm:"primaryKey;autoIncrement:false"`
    3. TypeID uint64 `gorm:"primaryKey;autoIncrement:false"`
    4. }