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
varUser=sequelize.define('User',{/* ... */})
varUser=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.
// 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:
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