ashishsanjayrao
ashishsanjayrao

Reputation: 101

Sequelize - Trying to fetch associated objects using 'include' for a belongsToMany relationship

I have a model called Experience, which has a belongsToMany association with the model Category through ExperienceCategory table. For an experience, I'm trying to fetch the Experience details with the Categories it belongs to. Some I'm using include: [{model: model.Category}] to achieve this. Now, the association is working and it is fetching the categories correctly, but the problem is that it's giving multiple objects of the Experience model, one for each Category. Below are some excerpts from my code:

model.Experience.findAll({
  include: [
    {
      model: model.Category
    }
  ]
}).then(experiences => {
  res.json({experiences: experiences});
})

This is how I have defined my associations:
experience.js:

Experience.belongsToMany(models.Category, {
  through: 'ExperienceCategory',
  foreignKey: 'experience_id',
});

category.js

Category.belongsToMany(models.Experience, {
  through: 'ExperienceCategory',
  foreignKey: 'category_id'
});

The response to the above query is:

{
  "experiences": [
    {
      "id": 23,
      "title": "Some title",
      "notes": "These are notes",
      "base_amount": 2000,
      "is_disabled": false,
      "is_deleted": false,
      "createdAt": "2019-05-27T12:24:06.736Z",
      "updatedAt": "2019-07-23T11:20:23.695Z",
      "Categories.id": 4,
      "Categories.name": "Trekking",
      "Categories.is_disabled": false,
      "Categories.is_deleted": false,
      "Categories.createdAt": "2019-05-14T11:07:40.287Z",
      "Categories.updatedAt": "2019-05-14T11:07:40.287Z",
      "Categories.ExperienceCategory.experience_id": 23,
      "Categories.ExperienceCategory.category_id": 4,
      "Categories.ExperienceCategory.createdAt": "2019-05-27T12:24:06.806Z",
      "Categories.ExperienceCategory.updatedAt": "2019-05-27T12:24:06.806Z"
    },
    {
      "id": 23,
      "title": "Some title",
      "notes": "These are notes",
      "base_amount": 2000,
      "is_disabled": false,
      "is_deleted": false,
      "createdAt": "2019-05-27T12:24:06.736Z",
      "updatedAt": "2019-07-23T11:20:23.695Z",
      "Categories.id": 5,
      "Categories.name": "Adventure",
      "Categories.is_disabled": false,
      "Categories.is_deleted": false,
      "Categories.createdAt": "2019-05-14T11:07:40.287Z",
      "Categories.updatedAt": "2019-05-14T11:07:40.287Z",
      "Categories.ExperienceCategory.experience_id": 23,
      "Categories.ExperienceCategory.category_id": 5,
      "Categories.ExperienceCategory.createdAt": "2019-05-27T12:24:06.806Z",
      "Categories.ExperienceCategory.updatedAt": "2019-05-27T12:24:06.806Z"
    }
  ]
}

You can see that for Experience ID 23 is repeated twice because there are two different Categories. Can I get the Categories in an array instead?

Thanks in advance.

Upvotes: 0

Views: 41

Answers (1)

Sébastien B
Sébastien B

Reputation: 445

Let say you have in your Experience model file the following association:

Experience.belongsToMany(models.Category, { through: 'ExperienceCategory', as: 'categories' });

You will then need to do the following to have a list of category in your experience request:

models.Experience.findAll({
 include: [
  { model: models.Category, as: 'categories' }
 ]
}).then(experiences => {
 res.json({experiences: experiences});
})

Upvotes: 1

Related Questions