InfoSphere/GORM 中文版/ 返回书籍
Associations

Has One

qianmoQqianmoQ· 更新于 2026-09-14· 阅读 8 分钟· 0 次阅读

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

Has One

A has one association sets up a one-to-one connection with another model, but with somewhat different semantics (and consequences). This association indicates that each instance of a model contains or possesses one instance of another model.

For example, if your application includes users and credit cards, and each user can only have one credit card.

Declare


type User struct {
  gorm.Model
  CreditCard CreditCard
}

type CreditCard struct {
  gorm.Model
  Number string
  UserID uint
}

Retrieve


func GetAll(db *gorm.DB) ([]User, error) {
  var users []User
  err := db.Model(&User{}).Preload("CreditCard").Find(&users).Error
  return users, err
}

Override Foreign Key

For a has one relationship, a foreign key field must also exist, the owner will save the primary key of the model belongs to it into this field.

The field’s name is usually generated with has one model’s type plus its primary key, for the above example it is UserID.

When you give a credit card to the user, it will save the User’s ID into its UserID field.

If you want to use another field to save the relationship, you can change it with tag foreignKey, e.g:

type User struct {
  gorm.Model
  CreditCard CreditCard `gorm:"foreignKey:UserName"`
  
}

type CreditCard struct {
  gorm.Model
  Number   string
  UserName string
}

Override References

By default, the owned entity will save the has one model’s primary key into a foreign key, you could change to save another field’s value, like using Name for the below example.

You are able to change it with tag references, e.g:

type User struct {
  gorm.Model
  Name       string     `gorm:"index"`
  CreditCard CreditCard `gorm:"foreignKey:UserName;references:Name"`
}

type CreditCard struct {
  gorm.Model
  Number   string
  UserName string
}

CRUD with Has One

Please checkout Association Mode for working with has one relations

Eager Loading

GORM allows eager loading has one associations with Preload or Joins, refer Preloading (Eager loading) for details

Self-Referential Has One

type User struct {
  gorm.Model
  Name      string
  ManagerID *uint
  Manager   *User
}

FOREIGN KEY Constraints

You can setup OnUpdate, OnDelete constraints with tag constraint, it will be created when migrating with GORM, for example:

type User struct {
  gorm.Model
  CreditCard CreditCard `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
}

type CreditCard struct {
  gorm.Model
  Number string
  UserID uint
}

You are also allowed to delete selected has one associations with Select when deleting, checkout Delete with Select for details

GitHub tag (latest SemVer)

评论

登录后参与评论

正在加载评论…