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

Commit df5c9386 by Mike DeLucco

Update associations.md

1 parent 6401eba4
Showing with 12 additions and 12 deletions
...@@ -51,7 +51,7 @@ Team.hasOne(Game, {as: 'AwayTeam', foreignKey : 'awayTeamId'}); ...@@ -51,7 +51,7 @@ Team.hasOne(Game, {as: 'AwayTeam', foreignKey : 'awayTeamId'});
Game.belongsTo(Team); Game.belongsTo(Team);
``` ```
To get the association working the other way around (so from `User` to `Project`), it's necessary to do this: To get the association working the other way around (so from `User` to `Project`), it's necessary to do this:
```js ```js
var User = sequelize.define('User', {/* ... */}) var User = sequelize.define('User', {/* ... */})
...@@ -68,7 +68,7 @@ Project.belongsTo(User) ...@@ -68,7 +68,7 @@ Project.belongsTo(User)
## One-To-Many associations ## One-To-Many associations
One-To-Many associations are connecting one source with multiple targets. The targets however are again connected to exactly one specific source. One-To-Many associations are connecting one source with multiple targets. The targets however are again connected to exactly one specific source.
```js ```js
var User = sequelize.define('User', {/* ... */}) var User = sequelize.define('User', {/* ... */})
var Project = sequelize.define('Project', {/* ... */}) var Project = sequelize.define('Project', {/* ... */})
...@@ -109,7 +109,7 @@ Person.belongsToMany(Person, { as: 'Children', through: 'PersonChildren' }) ...@@ -109,7 +109,7 @@ Person.belongsToMany(Person, { as: 'Children', through: 'PersonChildren' })
// This will create the table PersonChildren which stores the ids of the objects. // This will create the table PersonChildren which stores the ids of the objects.
``` ```
If you want additional attributes in your join table, you can define a model for the join table in sequelize, before you define the association, and then tell sequelize that it should use that model for joining, instead of creating a new one: If you want additional attributes in your join table, you can define a model for the join table in sequelize, before you define the association, and then tell sequelize that it should use that model for joining, instead of creating a new one:
```js ```js
User = sequelize.define('User', {}) User = sequelize.define('User', {})
...@@ -128,7 +128,7 @@ To add a new project to a user and set it's status, you pass an extra object to ...@@ -128,7 +128,7 @@ To add a new project to a user and set it's status, you pass an extra object to
user.addProject(project, { status: 'started' }) user.addProject(project, { status: 'started' })
``` ```
By default the code above will add ProjectId and UserId to the UserProjects table, and_remove any previsouly defined primary key attribute _- the table will be uniquely identified by the combination of the keys of the two tables, and there is no reason to have other PK columns. To enforce a primary key on the `UserProjects` model you can add it manually. By default the code above will add ProjectId and UserId to the UserProjects table, and_remove any previsouly defined primary key attribute _- the table will be uniquely identified by the combination of the keys of the two tables, and there is no reason to have other PK columns. To enforce a primary key on the `UserProjects` model you can add it manually.
```js ```js
UserProjects = sequelize.define('UserProjects', { UserProjects = sequelize.define('UserProjects', {
...@@ -169,7 +169,7 @@ This will add the functions `add/set/get Tasks` to user instances. ...@@ -169,7 +169,7 @@ This will add the functions `add/set/get Tasks` to user instances.
## Associating objects ## Associating objects
Because Sequelize is doing a lot of magic, you have to call `Sequelize.sync` after setting the associations! Doing so will allow you the following: Because Sequelize is doing a lot of magic, you have to call `Sequelize.sync` after setting the associations! Doing so will allow you the following:
```js ```js
Project.belongsToMany(Task) Project.belongsToMany(Task)
...@@ -202,7 +202,7 @@ project.getTasks({attributes: ['title']}).then(function(tasks) { ...@@ -202,7 +202,7 @@ project.getTasks({attributes: ['title']}).then(function(tasks) {
}) })
``` ```
To remove created associations you can just call the set method without a specific id: To remove created associations you can just call the set method without a specific id:
```js ```js
// remove the association with task1 // remove the association with task1
...@@ -226,7 +226,7 @@ project.addTask(task1).then(function() { ...@@ -226,7 +226,7 @@ project.addTask(task1).then(function() {
}) })
``` ```
You can of course also do it vice versa: You can of course also do it vice versa:
```js ```js
// project is associated with task1 and task2 // project is associated with task1 and task2
...@@ -235,14 +235,14 @@ task2.setProject(null).then(function() { ...@@ -235,14 +235,14 @@ task2.setProject(null).then(function() {
}) })
``` ```
For hasOne/belongsTo its basically the same: For hasOne/belongsTo its basically the same:
```js ```js
Task.hasOne(User, {as: "Author"}) Task.hasOne(User, {as: "Author"})
Task#setAuthor(anAuthor) Task#setAuthor(anAuthor)
``` ```
Adding associations to a relation with a custom join table can be done in two ways (continuing with the associations defined in the previous chapter): Adding associations to a relation with a custom join table can be done in two ways (continuing with the associations defined in the previous chapter):
```js ```js
// Either by adding a property with the name of the join table model to the object, before creating the association // Either by adding a property with the name of the join table model to the object, before creating the association
...@@ -265,7 +265,7 @@ u.setProjects([project1, project2], { status: 'active' }) ...@@ -265,7 +265,7 @@ u.setProjects([project1, project2], { status: 'active' })
// The code above will record inactive for project one, and active for project two in the join table // The code above will record inactive for project one, and active for project two in the join table
``` ```
When getting data on an association that has a custom join table, the data from the join table will be returned as a DAO instance: When getting data on an association that has a custom join table, the data from the join table will be returned as a DAO instance:
```js ```js
u.getProjects().then(function(projects) { u.getProjects().then(function(projects) {
...@@ -280,7 +280,7 @@ u.getProjects().then(function(projects) { ...@@ -280,7 +280,7 @@ u.getProjects().then(function(projects) {
}) })
``` ```
If you only need some of the attributes from the join table, you can provide an array with the attributes you want: If you only need some of the attributes from the join table, you can provide an array with the attributes you want:
```js ```js
// This will select only name from the Projects table, and only status from the UserProjects table // This will select only name from the Projects table, and only status from the UserProjects table
...@@ -288,7 +288,7 @@ user.getProjects({ attributes: ['name'], joinTableAttributes: ['status']}) ...@@ -288,7 +288,7 @@ user.getProjects({ attributes: ['name'], joinTableAttributes: ['status']})
``` ```
## Check associations ## Check associations
You can also check if an object is already associated with another one (N:M only). Here is how you'd do it: You can also check if an object is already associated with another one (N:M only). Here is how you'd do it:
```js ```js
// check if an object is one of associated ones: // check if an object is one of associated ones:
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!