Edit Page

Through associations

AKA "has many through"

Overview

#

Many-to-many through associations behave in the same way as many-to-many associations, except that in a many-to-many through association, the join table is created automatically. In a many-to-many through assocation, you define a model containing two fields that correspond to the two models you will be joining together. When defining an association, you will add the through key to show that the model should be used instead of the automatic join table.

Has many through example

#
// myApp/api/models/User.js
module.exports = {
  attributes: {
    name: {
      type: 'string'
    },
    pets:{
      collection: 'pet',
      via: 'owner',
      through: 'petuser'
    }
  }
}
// myApp/api/models/Pet.js
module.exports = {
  attributes: {
    name: {
      type: 'string'
    },
    color: {
      type: 'string'
    },
    owners:{
      collection: 'user',
      via: 'pet',
      through: 'petuser'
    }
  }
}
// myApp/api/models/PetUser.js
module.exports = {
  attributes: {
    owner:{
      model:'user'
    },
    pet: {
      model: 'pet'
    }
  }
}

By using the PetUser model, we can use .populate() on both the User model and Pet model just as we do in a normal many-to-many association.

Currently, if you would like to add additional information to the through table, it will not be available when calling .populate. To do this you will need to query the through model manually.

Is something missing?

If you notice something we've missed or could be improved on, please follow this link and submit a pull request to the sails repo. Once we merge it, the changes will be reflected on the website the next time it is deployed.

Concepts