英文:
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 
Userbelongs to oneCompanyonly → this is handled by the code
above - A 
Companyhas manyUser→ is this implied by the code
above? Or should I add somehow a relation O2M inCompany? 
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
Userhas 1+CreditCard→ this is handled by the code - 
A
CreditCardcan 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 aCreditCardis explicitly configured to belong to only one user? 
答案1
得分: 4
Q1: 根据您定义的结构体,Company 结构体不需要显式的一对多关系,但是当加载公司详情时,如果您想加载分配给该特定公司的所有用户,您需要添加该字段。它将需要一个额外的函数调用,比如 Preload 或 Joins,但您不需要显式定义这个关系。
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
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论