我应该明确创建一个与“属于”或“拥有多个”对称的关系吗?

huangapple go评论89阅读模式
英文:

Should I explicitly create a relation symmetrical to "Belongs To" or "Has Many"?

问题

我对ORM(和GORM)还不熟悉,如果这是一个显而易见的问题,我向你道歉,因为文档中似乎没有涉及到这个问题。

我将使用文档中的示例作为我的问题基础。

问题1:属于关系

// `User` 属于 `Company`,`CompanyID` 是外键
type User struct {
  gorm.Model
  Name      string
  CompanyID int
  Company   Company
}

type Company struct {
  ID   int
  Name string
}
  • 一个 User 只属于一个 Company → 这在上面的代码中已经处理了
  • 一个 Company 有多个 User → 这是否可以从上面的代码中推断出来?或者我应该在 Company 中以某种方式添加一个一对多的关系?

问题2:拥有多个关系

// User 拥有多个 CreditCards,UserID 是外键
type User struct {
  gorm.Model
  CreditCards []CreditCard
}

type CreditCard struct {
  gorm.Model
  Number string
  UserID uint
}
  • 一个 User 有一个或多个 CreditCard → 这在代码中已经处理了
  • 一个 CreditCard 可以属于多个用户(比如,一个共享的家庭信用卡)→ 这是否可以推断出来?(如果不能:如何设置一对多的关系)。
    或者,这是一个 CreditCard 明确配置为仅属于一个用户的情况?
英文:

I am new to ORM (and GORM) so apologies if this is an obvious question, but it does not seem to be covered by the documentation.

I will be using the examples from the documentation as a base to my questions

Question 1: Belongs To

// `User` belongs to `Company`, `CompanyID` is the foreign key
type User struct {
  gorm.Model
  Name      string
  CompanyID int
  Company   Company
}

type Company struct {
  ID   int
  Name string
}
  • A User belongs to one Company only → this is handled by the code
    above
  • A Company has many Useris this implied by the code
    above?
    Or should I add somehow a relation O2M in Company?

Question 2: Has Many

// User has many CreditCards, UserID is the foreign key
type User struct {
  gorm.Model
  CreditCards []CreditCard
}

type CreditCard struct {
  gorm.Model
  Number string
  UserID uint
}
  • A User has 1+ CreditCard→ this is handled by the code

  • A CreditCard can belong to several users (say, a shared family CC) → is it implied? (if not: how to set up the O2M relationship).
    Or is it, instead, a case where a CreditCard is explicitly configured to belong to only one user?

答案1

得分: 4

Q1: 根据您定义的结构体,Company 结构体不需要显式的一对多关系,但是当加载公司详情时,如果您想加载分配给该特定公司的所有用户,您需要添加该字段。它将需要一个额外的函数调用,比如 PreloadJoins,但您不需要显式定义这个关系。

type Company struct {
  ID    int
  Name  string
  Users []User
}

Q2: 目前关系的定义方式是,一个 CreditCard 属于一个用户。如果您想要一个多对多关系,您需要指定关系表。关于此的更多文档可以在这里找到,但大致如下所示:

type User struct {
  gorm.Model
  CreditCards []CreditCard `gorm:"many2many:users_creditcards"`
}

type CreditCard struct {
  gorm.Model
  Number string
}
英文:

Q1: Based on how you defined your structs, you don't need an explicit O2M relationship in the Company struct, but when loading Company details, if you want to load all users that are assigned to that specific company, you need to add that field as well. It will need an additional function call like Preload or Joins, but you shouldn't need an explicit definition of this relationship.

type Company struct {
  ID   int
  Name string
  Users []User
}

Q2: The way the relationship is defined now, it is configured so that a CreditCard belongs to only one user. If you want a many2many relationship, you need to specify the relation table. There is more documentation on it here, but it should look something like this:

type User struct {
  gorm.Model
  CreditCards []CreditCard `gorm:"many2many:users_creditcards"`
}

type CreditCard struct {
  gorm.Model
  Number string
}

huangapple
  • 本文由 发表于 2022年3月17日 16:10:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/71508941.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定