属于关系
3 分钟阅读
属于关系 Belongs To
A belongs to
association sets up a one-to-one connection with another model, such that each instance of the declaring model “belongs to” one instance of the other model.
一个belongs to
关联设置了一个模型与另一个模型之间的一对一连接,使得声明模型的每个实例“属于”另一个模型的一个实例。
For example, if your application includes users and companies, and each user can be assigned to exactly one company, the following types represent that relationship. Notice here that, on the User
object, there is both a CompanyID
as well as a Company
. By default, the CompanyID
is implicitly used to create a foreign key relationship between the User
and Company
tables, and thus must be included in the User
struct in order to fill the Company
inner struct.
例如,如果你的应用包括用户和公司,并且每个用户可以被分配给一个公司,那么以下类型表示这种关系。请注意,在User
对象上,有一个CompanyID
以及一个Company
。默认情况下,CompanyID
隐式地用于创建User
和Company
表之间的外键关系,因此必须包含在User
结构体中才能填充Company
内部结构体。
|
|
Refer to Eager Loading for details on populating the inner struct.
有关填充内部结构的详细信息,请参阅预加载(预加载)。
覆盖外键 Override Foreign Key
To define a belongs to relationship, the foreign key must exist, the default foreign key uses the owner’s type name plus its primary field name.
要定义一个属于关系,外键必须存在,默认的外键使用拥有者的类型名称加上其主字段名称。
For the above example, to define the User
model that belongs to Company
, the foreign key should be CompanyID
by convention
要定义一个属于关系,外键必须存在,默认的外键使用拥有者的类型名称加上其主字段名称。
GORM provides a way to customize the foreign key, for example:
GORM提供了一个自定义外键的方法,例如:
|
|
覆盖引用 Override References
For a belongs to relationship, GORM usually uses the owner’s primary field as the foreign key’s value, for the above example, it is Company
‘s field ID
.
对于一个属于关系,GORM通常使用拥有者的主字段作为外键的值,对于上面的示例,它是Company
的字段ID
。
When you assign a user to a company, GORM will save the company’s ID
into the user’s CompanyID
field.
当你将用户分配给一家公司时,GORM将公司的ID
保存到用户的CompanyID
字段中。
You are able to change it with tag references
, e.g:
你可以使用标签references
更改它,例如:
|
|
NOTE GORM usually guess the relationship as
has one
if override foreign key name already exists in owner’s type, we need to specifyreferences
in thebelongs to
relationship.注意 GORM通常在拥有者的类型中已经存在覆盖外键名称的关系时猜测关系为
has one
,我们需要在belongs to
关系中指定references
。
|
|
与属于关系的CRUD操作 CRUD with Belongs To
Please checkout Association Mode for working with belongs to relations
请查看关联模式以处理属于关系
预加载(预加载) Eager Loading
GORM allows eager loading belongs to associations with Preload
or Joins
, refer Preloading (Eager loading) for details
GORM允许通过Preload
或Joins
预加载属于关系,请参阅预加载(预加载)以获取详细信息
FOREIGN KEY约束 FOREIGN KEY Constraints
You can setup OnUpdate
, OnDelete
constraints with tag constraint
, it will be created when migrating with GORM, for example:
你可以使用带有标签constraint
的OnUpdate
和OnDelete
约束来设置OnUpdate
、OnDelete
约束,当使用GORM进行迁移时,它将被创建,例如:
|
|