Skip to main content
Version: v6 - stable

与关联一起创建

如果所有元素都是新的,则可以一步创建具有嵌套关联的实例。

¥An instance can be created with nested association in one step, provided all elements are new.

相反,当前无法执行涉及嵌套对象的更新和删除。为此,你必须明确执行每个单独的操作。

¥In contrast, performing updates and deletions involving nested objects is currently not possible. For that, you will have to perform each separate action explicitly.

BelongsTo / HasMany / HasOne 关联

¥BelongsTo / HasMany / HasOne association

考虑以下模型:

¥Consider the following models:

class Product extends Model {}
Product.init({
title: Sequelize.STRING
}, { sequelize, modelName: 'product' });
class User extends Model {}
User.init({
firstName: Sequelize.STRING,
lastName: Sequelize.STRING
}, { sequelize, modelName: 'user' });
class Address extends Model {}
Address.init({
type: DataTypes.STRING,
line1: Sequelize.STRING,
line2: Sequelize.STRING,
city: Sequelize.STRING,
state: Sequelize.STRING,
zip: Sequelize.STRING,
}, { sequelize, modelName: 'address' });

// We save the return values of the association setup calls to use them later
Product.User = Product.belongsTo(User);
User.Addresses = User.hasMany(Address);
// Also works for `hasOne`

可以通过以下方式一步创建新的 ProductUser 和一个或多个 Address

¥A new Product, User, and one or more Address can be created in one step in the following way:

return Product.create({
title: 'Chair',
user: {
firstName: 'Mick',
lastName: 'Broadstone',
addresses: [{
type: 'home',
line1: '100 Main St.',
city: 'Austin',
state: 'TX',
zip: '78704'
}]
}
}, {
include: [{
association: Product.User,
include: [ User.Addresses ]
}]
});

观察 Product.create 调用中 include 选项的用法。这对于 Sequelize 理解你想要创建的关联是必要的。

¥Observe the usage of the include option in the Product.create call. That is necessary for Sequelize to understand what you are trying to create along with the association.

注意:这里,我们的用户模型称为 user,带有小写的 u - 这意味着对象中的属性也应该是 user。如果给 sequelize.define 指定的名称是 User,则对象中的键也应该是 User。对于 addresses 也是如此,只不过它是复数形式的 hasMany 关联。

¥Note: here, our user model is called user, with a lowercase u - This means that the property in the object should also be user. If the name given to sequelize.define was User, the key in the object should also be User. Likewise for addresses, except it's pluralized being a hasMany association.

BelongsTo 与别名的关联

¥BelongsTo association with an alias

可以扩展前面的示例以支持关联别名。

¥The previous example can be extended to support an association alias.

const Creator = Product.belongsTo(User, { as: 'creator' });

return Product.create({
title: 'Chair',
creator: {
firstName: 'Matt',
lastName: 'Hansen'
}
}, {
include: [ Creator ]
});

HasMany / BelongsToMany 关联

¥HasMany / BelongsToMany association

让我们介绍一下将一个产品与多个标签关联起来的能力。设置模型可能如下所示:

¥Let's introduce the ability to associate a product with many tags. Setting up the models could look like:

class Tag extends Model {}
Tag.init({
name: Sequelize.STRING
}, { sequelize, modelName: 'tag' });

Product.hasMany(Tag);
// Also works for `belongsToMany`.

现在我们可以通过以下方式创建具有多个标签的产品:

¥Now we can create a product with multiple tags in the following way:

Product.create({
id: 1,
title: 'Chair',
tags: [
{ name: 'Alpha'},
{ name: 'Beta'}
]
}, {
include: [ Tag ]
})

并且,我们可以修改此示例以支持别名:

¥And, we can modify this example to support an alias as well:

const Categories = Product.hasMany(Tag, { as: 'categories' });

Product.create({
id: 1,
title: 'Chair',
categories: [
{ id: 1, name: 'Alpha' },
{ id: 2, name: 'Beta' }
]
}, {
include: [{
association: Categories,
as: 'categories'
}]
})