AKA "Belongs To"
A one way association is where a model is associated with another model. You could query that model and populate to get the associated model. You can't however query the associated model and populate to get the associating model.
In this example, we are associating a User
with a Pet
but not a Pet
with a User
.
// myApp/api/models/Pet.js
module.exports = {
attributes: {
name: {
type: 'string'
},
color: {
type: 'string'
}
}
}
// myApp/api/models/User.js
module.exports = {
attributes: {
name: {
type: 'string'
},
age: {
type: 'integer'
},
pony:{
model: 'pet'
}
}
}
Now that the association is setup, you can populate the pony association.
User.find({ name:'Mike' })
.populate('pony')
.exec(function(err, users) {
// The users object would look something like:
// [{
// name: 'Mike',
// age: 21,
// pony: {
// name: 'Pinkie Pie',
// color: 'pink',
// id: 5,
// createdAt: Tue Feb 11 2014 15:45:33 GMT-0600 (CST),
// updatedAt: Tue Feb 11 2014 15:45:33 GMT-0600 (CST)
// },
// createdAt: Tue Feb 11 2014 15:48:53 GMT-0600 (CST),
// updatedAt: Tue Feb 11 2014 15:48:53 GMT-0600 (CST),
// id: 1
// }]
For a more detailed description of this type of association, see the Waterline Docs
Because we have only formed an association on one of the models, a
Pet
has no restrictions on the number ofUser
models it can belong to. If we wanted to, we could change this and associate thePet
with exactly oneUser
and theUser
with exactly onePet
.