不要怂,就是干,撸起袖子干!

Commit a89fda26 by Tim Scott Committed by Sushant

Document creating with deep nesting (#6791)

* Update associations.md

* Update associations.md

* Update associations.md

* Update associations.md

* Update associations.md

Another correction
1 parent aa13dc6d
Showing with 25 additions and 6 deletions
...@@ -727,7 +727,7 @@ Trainer.hasMany(Series); ...@@ -727,7 +727,7 @@ Trainer.hasMany(Series);
An instance can be created with nested association in one step, provided all elements are new. An instance can be created with nested association in one step, provided all elements are new.
### Creating elements of a "BelongsTo" or "HasOne" association ### Creating elements of a "BelongsTo", "Has Many" or "HasOne" association
Consider the following models: Consider the following models:
...@@ -739,26 +739,45 @@ var User = this.sequelize.define('user', { ...@@ -739,26 +739,45 @@ var User = this.sequelize.define('user', {
first_name: Sequelize.STRING, first_name: Sequelize.STRING,
last_name: Sequelize.STRING last_name: Sequelize.STRING
}); });
var Address = this.sequelize.define('address', {
type: Sequelize.STRING,
line_1: Sequelize.STRING,
line_2: Sequelize.STRING,
city: Sequelize.STRING,
state: Sequelize.STRING,
zip: Sequelize.STRING,
});
Product.belongsTo(User); var Product.User = Product.belongsTo(User);
var User.Addresses = User.hasMany(Address);
// Also works for `hasOne` // Also works for `hasOne`
``` ```
A new `Product` and `User` can be created in one step in the following way: A new `Product`, `User`, and one or more `Address` can be created in one step in the following way:
```js ```js
return Product.create({ return Product.create({
title: 'Chair', title: 'Chair',
user: { user: {
first_name: 'Mick', first_name: 'Mick',
last_name: 'Broadstone' last_name: 'Broadstone',
addresses: [{
type: 'home',
line_1: '100 Main St.',
city: 'Austin',
state: 'TX',
zip: '78704'
}]
} }
}, { }, {
include: [ User ] include: [{
association: Product.User,
include: [ User.Addresses ]
}]
}); });
``` ```
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`. 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.
### Creating elements of a "BelongsTo" association with an alias ### Creating elements of a "BelongsTo" association with an alias
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!