InfoSphere/GORM 中文版/ 返回书籍
Advanced Topics

Composite Primary Key

qianmoQqianmoQ· 更新于 2026-09-15· 阅读 2 分钟· 0 次阅读

登录后可跨设备保存划线和私人笔记登录

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

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:

type Product struct {
  CategoryID uint64 `gorm:"primaryKey;autoIncrement:false"`
  TypeID     uint64 `gorm:"primaryKey;autoIncrement:false"`
}

GitHub tag (latest SemVer)

评论

登录后参与评论

正在加载评论…