复合主键

原文:https://gorm.io/docs/composite_primary_key.html

Set multiple fields as primary key creates composite primary key, for example:

​ 将多个字段设置为主键,创建复合主键,例如:

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

Note integer PrioritizedPrimaryField enables AutoIncrement by default, to disable it, you need to turn off autoIncrement for the int fields:

注意:整数PrioritizedPrimaryField默认启用AutoIncrement,要禁用它,您需要为int字段关闭autoIncrement

1
2
3
4
type Product struct {
  CategoryID uint64 `gorm:"primaryKey;autoIncrement:false"`
  TypeID     uint64 `gorm:"primaryKey;autoIncrement:false"`
}
最后修改 October 10, 2024: 更新 (a4b8f85)