一对多关联
2 分钟阅读
原文:https://gobuffalo.io/documentation/database/relations-one-to-many/
One to Many Associations 一对多关联
In this chapter, you’ll learn how to write a one to many association in Pop.
在本章中,您将学习如何在 Pop 中编写一对多关联。
Tags 标签
One to many associations work using a pair of tags:
一对多关联使用一对标签:
belongs_to
for the model with the foreign key.belongs_to
用于具有外键的模型。has_many
for the model without the foreign key (the one with the slice).has_many
用于没有外键的模型(带有切片的模型)。
Example 示例
|
|
Custom Association Order 自定义关联顺序
Since has_many
is mapped to a slice, you’ll probably want to customize the order of this slice. order_by
tag allows you to indicate the order for the association when loading it:
由于 has_many
映射到切片,因此您可能希望自定义此切片的顺序。 order_by
标签允许您在加载关联时指示关联的顺序:
|
|
The format to use is order_by:"<column_name> <asc | desc>"
.
要使用的格式是 order_by:"<column_name> <asc | desc>"
。
Customize Foreign Keys Lookup 自定义外键查找
By default, has_many
will fetch related records using a convention for the foreign key column. In our previous example, the fruits
table (mapped to the Fruit
struct) contains a tree_id
foreign key column which references the ID of the tree the fruit is attached to.
默认情况下, has_many
将使用外键列的约定来获取相关记录。在我们的上一个示例中, fruits
表(映射到 Fruit
结构)包含一个 tree_id
外键列,该列引用水果所附树的 ID。
You can use the fk_id
tag to customize this foreign key column:
您可以使用 fk_id
标记来自定义此外键列:
|
|
Here, the relation will be looked up using the column custom_tree_id
in the fruits
table, instead of the default tree_id
one.
在此,将使用 fruits
表中的列 custom_tree_id
来查找关系,而不是默认的 tree_id
列。
This can be really useful when you have structs with multiple fields pointing to the same model:
当您有多个字段指向同一模型的结构时,这非常有用: 与 Pop 的关联:1 对 n - 一篇关于 Pop 中 1 对 n 关联的文章。
|
|
Related Content 相关内容
- Associations with Pop: 1 to n - An article about 1 to n associations in Pop.