With Sequelize you can also specify associations between multiple classes. Doing so will help you to easily access and set those associated objects. The library therefore provides for each defined class different methods, which are explained in the following chapters.
With Sequelize you can also specify associations between multiple classes. Doing so will help you to easily access and set those associated objects. The library therefore provides for each defined class different methods, which are explained in the following chapters.
**Note: **Associations with models that use custom primaryKeys (so not the field 'id') are currently unsupported.
## One-To-One associations
## One-To-One associations
One-To-One associations are connecting one source with exactly one target. In order to define a proper database schema, Sequelize utilizes the methods `belongsTo` and `hasOne`. You can use them as follows:
One-To-One associations are connecting one source with exactly one target. In order to define a proper database schema, Sequelize utilizes the methods `belongsTo` and `hasOne`. You can use them as follows:
...
@@ -47,19 +45,11 @@ Person#setFather
...
@@ -47,19 +45,11 @@ Person#setFather
Person#getFather
Person#getFather
// If you need to join a table twice you can double join the same table
// If you need to join a table twice you can double join the same table
// you can now access the currently saved task with the variable anotherTask... nice!
// you can now access the currently saved task with the variable anotherTask... nice!
}).error(function(error){
}).catch(function(error){
// Ooops, do some error-handling
// Ooops, do some error-handling
})
})
```
```
...
@@ -58,7 +58,7 @@ Task
...
@@ -58,7 +58,7 @@ Task
Besides constructing objects, that needs an explicit save call to get stored in the database, there is also the possibility to do all those steps with one single command. It's called `create`.
Besides constructing objects, that needs an explicit save call to get stored in the database, there is also the possibility to do all those steps with one single command. It's called `create`.
It is also possible to define which attributes can be set via the create method. This can be especially very handy if you create database entries based on a form which can be filled by a user. Using that would for example allow you to restrict the `User` model to set only a username and an address but not an admin flag:
It is also possible to define which attributes can be set via the create method. This can be especially very handy if you create database entries based on a form which can be filled by a user. Using that would for example allow you to restrict the `User` model to set only a username and an address but not an admin flag:
@@ -79,12 +79,12 @@ Now lets change some values and save changes to the database..&per
...
@@ -79,12 +79,12 @@ Now lets change some values and save changes to the database..&per
```js
```js
// way 1
// way 1
task.title='a very different title now'
task.title='a very different title now'
task.save().success(function(){})
task.save().then(function(){})
// way 2
// way 2
task.updateAttributes({
task.updateAttributes({
title:'a very different title now'
title:'a very different title now'
}).success(function(){})
}).then(function(){})
```
```
It's also possible to define which attributes should be saved when calling`save`, by passing an array of column names. This is useful when you set attributes based on a previously defined object. E.g. if you get the values of an object via a form of a web app. Furthermore this is used internally for `updateAttributes`. This is how it looks like:
It's also possible to define which attributes should be saved when calling`save`, by passing an array of column names. This is useful when you set attributes based on a previously defined object. E.g. if you get the values of an object via a form of a web app. Furthermore this is used internally for `updateAttributes`. This is how it looks like:
]).success(function(){// Notice: There are no arguments here, as of right now you'll have to...
]).then(function(){// Notice: There are no arguments here, as of right now you'll have to...
User.findAll().success(function(users){
User.findAll().then(function(users){
console.log(users)// ... in order to get the array of user objects
console.log(users)// ... in order to get the array of user objects
})
})
})
})
...
@@ -153,13 +153,13 @@ Task.bulkCreate([
...
@@ -153,13 +153,13 @@ Task.bulkCreate([
{subject:'programming',status:'executing'},
{subject:'programming',status:'executing'},
{subject:'reading',status:'executing'},
{subject:'reading',status:'executing'},
{subject:'programming',status:'finished'}
{subject:'programming',status:'finished'}
]).success(function(){
]).then(function(){
Task.update(
Task.update(
{status:'inactive'}/* set attributes' value */,
{status:'inactive'}/* set attributes' value */,
{where:{subject:'programming'}}/* where criteria */
{where:{subject:'programming'}}/* where criteria */
).success(function(affectedRows){
).then(function(affectedRows){
// affectedRows will be 2
// affectedRows will be 2
Task.findAll().success(function(tasks){
Task.findAll().then(function(tasks){
console.log(tasks)// the 'programming' tasks will both have a status of 'inactive'
console.log(tasks)// the 'programming' tasks will both have a status of 'inactive'
})
})
})
})
...
@@ -173,13 +173,13 @@ Task.bulkCreate([
...
@@ -173,13 +173,13 @@ Task.bulkCreate([
{subject:'programming',status:'executing'},
{subject:'programming',status:'executing'},
{subject:'reading',status:'executing'},
{subject:'reading',status:'executing'},
{subject:'programming',status:'finished'}
{subject:'programming',status:'finished'}
]).success(function(){
]).then(function(){
Task.destroy(
Task.destroy(
{where:{subject:'programming'}}/* where criteria */,
{where:{subject:'programming'}}/* where criteria */,
{truncate:true/* truncate the whole table, ignoring where criteria */}/* options */
{truncate:true/* truncate the whole table, ignoring where criteria */}/* options */
).success(function(affectedRows){
).then(function(affectedRows){
// affectedRows will be 2
// affectedRows will be 2
Task.findAll().success(function(tasks){
Task.findAll().then(function(tasks){
console.log(tasks)// no programming, just reading :(
console.log(tasks)// no programming, just reading :(
})
})
})
})
...
@@ -192,7 +192,7 @@ If you are accepting values directly from the user, it might be beneficial to li
...
@@ -192,7 +192,7 @@ If you are accepting values directly from the user, it might be beneficial to li
User.bulkCreate([
User.bulkCreate([
{username:'foo'},
{username:'foo'},
{username:'bar',admin:true}
{username:'bar',admin:true}
],{fields:['username']}).success(function(){
],{fields:['username']}).then(function(){
// nope bar, you can't be admin!
// nope bar, you can't be admin!
})
})
```
```
...
@@ -219,7 +219,7 @@ Tasks.bulkCreate([
...
@@ -219,7 +219,7 @@ Tasks.bulkCreate([
{name:'foo',code:'123'},
{name:'foo',code:'123'},
{code:'1234'},
{code:'1234'},
{name:'bar',code:'1'}
{name:'bar',code:'1'}
],{validate:true}).error(function(errors){
],{validate:true}).catch(function(errors){
/* console.log(errors) would look like:
/* console.log(errors) would look like:
[
[
{ record:
{ record:
...
@@ -247,7 +247,7 @@ If you log an instance you will notice, that there is a lot of additional
...
@@ -247,7 +247,7 @@ If you log an instance you will notice, that there is a lot of additional
Person.create({
Person.create({
name:'Rambow',
name:'Rambow',
firstname:'John'
firstname:'John'
}).success(function(john){
}).then(function(john){
console.log(john.values)
console.log(john.values)
})
})
...
@@ -268,11 +268,11 @@ Person.create({
...
@@ -268,11 +268,11 @@ Person.create({
If you need to get your instance in sync, you can use the method`reload`. It will fetch the current data from the database and overwrite the attributes of the model on which the method has been called on.
If you need to get your instance in sync, you can use the method`reload`. It will fetch the current data from the database and overwrite the attributes of the model on which the method has been called on.
@@ -967,7 +967,7 @@ Notice that the accessor is plural. This is because the association is ma
...
@@ -967,7 +967,7 @@ Notice that the accessor is plural. This is because the association is ma
If an association is aliased (using the`as`option), you_must_specify this alias when including the model. Notice how the user's`Tool`s are aliased as`Instruments`above. In order to get that right you have to specify the model you want to load, as well as the alias:
If an association is aliased (using the`as`option), you_must_specify this alias when including the model. Notice how the user's`Tool`s are aliased as`Instruments`above. In order to get that right you have to specify the model you want to load, as well as the alias: