2

I'm trying to multiple populate in sails js using sails-hook-deep-orm. I want to filter the province id. I am using the latest version of Sails and Node. And I using MySQL for database.

This is my model :

// Province.js
attributes: {
    name: {
      type: 'string',
      required: true
    },
    description: {
      type: 'string',
      required: false
    },
    cities: {
      collection: 'city',
      via: 'province'
    }
}

// City.js
attributes: {
    name: {
      type: 'string',
      required: true
    },
    description: {
      type: 'string',
      required: false
    },
    province: {
      model: 'province'
    },
    communities: {
      collection: 'community',
      via: 'city'
    }
}

// Community.js
attributes: {
    name: {
      type: 'string',
      required: true
    },
    description: {
      type: 'string',
      required: false
    },
    cities: {
      collection: 'city',
      via: 'province'
    }
}

I have try with :

Community.find()
  .populate('city.province', {'city.province.id' : 1});

Community.find()
  .populate('city.province', { where: { 'city.province.id': 1 } });

The code still not filtering. I Hope this case can be solved. Thank you :)

Luthfi Arifin
  • 21
  • 1
  • 2

1 Answers1

2

When you populate an association of your object, the name needs to be the name of the association in the parent object and not the name of the model you are referring to.

In your situation, Community has the association named 'cities'.

The next step is to filter the list of cities, this is the where statement as the second argument of the populate.

Community.find()
 .populate('cities', { 
    where: { 
      province: 1 
    }
  }
)
  • 2
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. [From Review](/review/low-quality-posts/25775719) – double-beep Apr 04 '20 at 20:06
  • i'm sorry, i give wrong model in community.js attributes: { name: { type: 'string', required: true }, description: { type: 'string', required: false }, city: { model: 'city' }, }, this is the real model, this is one to one, thanks :) – Luthfi Arifin Apr 12 '20 at 17:59