关联
Sequelize 支持标准关联:一对一、一对多 和 多对多。
¥Sequelize supports the standard associations: One-To-One, One-To-Many and Many-To-Many.
为此,Sequelize 提供了四种类型的关联,应将它们组合起来创建它们:
¥To do this, Sequelize provides four types of associations that should be combined to create them:
-
HasOne
关联¥The
HasOne
association -
BelongsTo
关联¥The
BelongsTo
association -
HasMany
关联¥The
HasMany
association -
BelongsToMany
关联¥The
BelongsToMany
association
本指南将首先解释如何定义这四种关联类型,然后解释如何将它们组合起来定义三种标准关联类型(一对一、一对多 和 多对多)。
¥The guide will start explaining how to define these four types of associations, and then will follow up to explain how to combine those to define the three standard association types (One-To-One, One-To-Many and Many-To-Many).
定义 Sequelize 关联
¥Defining the Sequelize associations
四种关联类型的定义方式非常相似。假设我们有两个型号:A
和 B
。告诉 Sequelize 你想要两者之间的关联只需要一个函数调用:
¥The four association types are defined in a very similar way. Let's say we have two models, A
and B
. Telling Sequelize that you want an association between the two needs just a function call:
const A = sequelize.define('A' /* ... */);
const B = sequelize.define('B' /* ... */);
A.hasOne(B); // A HasOne B
A.belongsTo(B); // A BelongsTo B
A.hasMany(B); // A HasMany B
A.belongsToMany(B, { through: 'C' }); // A BelongsToMany B through the junction table C
它们都接受选项对象作为第二个参数(前三个参数是可选的,对于至少包含 through
属性的 belongsToMany
是强制的):
¥They all accept an options object as a second parameter (optional for the first three, mandatory for belongsToMany
containing at least the through
property):
A.hasOne(B, {
/* options */
});
A.belongsTo(B, {
/* options */
});
A.hasMany(B, {
/* options */
});
A.belongsToMany(B, { through: 'C' /* options */ });
定义关联的顺序是相关的。换句话说,对于这四种情况,顺序很重要。在上面的所有示例中,A
称为源模型,B
称为目标模型。这个术语很重要。
¥The order in which the association is defined is relevant. In other words, the order matters, for the four cases. In all examples above, A
is called the source model and B
is called the target model. This terminology is important.
A.hasOne(B)
关联意味着 A
和 B
之间存在一对一关系,外键在目标模型 (B
) 中定义。
¥The A.hasOne(B)
association means that a One-To-One relationship exists between A
and B
, with the foreign key being defined in the target model (B
).
A.belongsTo(B)
关联意味着 A
和 B
之间存在一对一关系,外键在源模型 (A
) 中定义。
¥The A.belongsTo(B)
association means that a One-To-One relationship exists between A
and B
, with the foreign key being defined in the source model (A
).
A.hasMany(B)
关联意味着 A
和 B
之间存在一对多关系,外键在目标模型 (B
) 中定义。
¥The A.hasMany(B)
association means that a One-To-Many relationship exists between A
and B
, with the foreign key being defined in the target model (B
).
这三个调用将导致 Sequelize 自动将外键添加到适当的模型中(除非它们已经存在)。
¥These three calls will cause Sequelize to automatically add foreign keys to the appropriate models (unless they are already present).
A.belongsToMany(B, { through: 'C' })
关联意味着 A
和 B
之间存在多对多关系,使用表 C
作为 连接表,该表将具有外键(例如 aId
和 bId
)。Sequelize 将自动创建此模型 C
(除非它已经存在)并在其上定义适当的外键。
¥The A.belongsToMany(B, { through: 'C' })
association means that a Many-To-Many relationship exists between A
and B
, using table C
as junction table, which will have the foreign keys (aId
and bId
, for example). Sequelize will automatically create this model C
(unless it already exists) and define the appropriate foreign keys on it.
注意:在上面的 belongsToMany
示例中,字符串 ('C'
) 被传递给 through 选项。在这种情况下,Sequelize 会自动生成具有此名称的模型。但是,如果你已经定义了模型,也可以直接传递模型。
¥Note: In the examples above for belongsToMany
, a string ('C'
) was passed to the through option. In this case, Sequelize automatically generates a model with this name. However, you can also pass a model directly, if you have already defined it.
这些是每种类型的关联所涉及的主要思想。然而,这些关系通常成对使用,以便更好地使用 Sequelize。这一点稍后会看到。
¥These are the main ideas involved in each type of association. However, these relationships are often used in pairs, in order to enable better usage with Sequelize. This will be seen later on.
创建标准关系
¥Creating the standard relationships
如前所述,Sequelize 关联通常是成对定义的。总之:
¥As mentioned, usually the Sequelize associations are defined in pairs. In summary:
-
要创建一对一关系,需要同时使用
hasOne
和belongsTo
关联;¥To create a One-To-One relationship, the
hasOne
andbelongsTo
associations are used together; -
要创建一对多关系,需要同时使用
hasMany
和belongsTo
关联;¥To create a One-To-Many relationship, the
hasMany
andbelongsTo
associations are used together; -
要创建多对多关系,需要一起使用两个
belongsToMany
调用。¥To create a Many-To-Many relationship, two
belongsToMany
calls are used together.-
注意:还有一种超级多对多关系,它同时使用六个关联,这将在 高级多对多关系指南 中讨论。
¥Note: there is also a Super Many-To-Many relationship, which uses six associations at once, and will be discussed in the Advanced Many-to-Many relationships guide.
-
这一切都将在接下来详细地看到。使用这些对而不是单个关联的优点将在本章末尾讨论。
¥This will all be seen in detail next. The advantages of using these pairs instead of one single association will be discussed in the end of this chapter.
一对一的关系
¥One-To-One relationships
哲学
¥Philosophy
在深入研究使用 Sequelize 的各个方面之前,退后一步考虑一下一对一关系会发生什么是很有用的。
¥Before digging into the aspects of using Sequelize, it is useful to take a step back to consider what happens with a One-To-One relationship.
假设我们有两个型号:Foo
和 Bar