0

I'm bangin' my head against the wall in the last hours, and I can't figure out a solution to my problem. In my sails models, I have 2 one-to-many associations. 'A' model can have many 'B', and 'B' model can have many 'C'. In my controller, when I do a.find().populate('b') (...) it returns me the entire collection of A model, but populate each entry of A model with only one B, which doesn't match the current dataset I have in my database(mysql). And doesn't populate the C entries in the B model. In other words, I'm trying to achieve something like nested population.
It's something wrong with the Controller code, right? How can I make this work? Thanks in advance!

EDIT:

Company.js

module.exports = {

identity: 'company',

attributes: {

    name: {
        type: 'string',
        required: true
    },

    address: {
        type: 'string',
        required: true
    },

    zip_code: {
        type: 'string',
        required: true
    },

    city: {
        type: 'string',
        required: true
    },

    nif: {
        type: 'integer',
        required: true,
        minLength: 9
    },

    country: {
        type: 'string',
        required: true
    },

    phone_number: {
        type: 'string',
        required: true
    },

    email: {
        type: 'email',
        required: true
    },

    facilities: {
        collection: 'facility',
        references: 'facility',
        on: 'id',
        via: 'company'
    }
  }
};

Facility.js

module.exports = {

identity: 'facility',

attributes: {

    company: {
        columnName: 'id_company',
        model: 'company'
    },

    warehouses: {
        collection: 'warehouse',
        references: 'warehouse',
        on: 'id',
        via: 'facility'
    },

    name: {
        type: 'string',
        required: true
    },

    address: {
        type: 'string',
        required: true
    },

    zip_code: {
        type: 'string',
        required: true
    },

    city: {
        type: 'string',
        required: true
    },

    country: {
        type: 'string',
        required: true
    },

    phone_number: {
        type: 'string',
    },

    email: {
        type: 'email',
    },

    longitude: {
        type: 'float',
    },

    latitude: {
        type: 'float'
    }

  }
};

Warehouse.js

module.exports = {

identity: 'warehouse',

attributes: {

    facility: {
        columnName: 'id_facility',
        model: 'facility',
    },

    name: {
        type: 'string',
        required: true
    },

    longitude: {
        type: 'float',
    },

    latitude: {
        type: 'float'
    }

  }
};

MainController's relevant code:

companies: function(req, res) {
    company.find().populate('facilities').exec(function(err, comp){
        var error = '';
        if(err){
            error = 'Unable to retrieve the requested information. Try again later and, if the problem persists, contact the platform administrator.';
        } else if(!comp[0]) {
            error = 'There\'s no company data inserted.';
        }
        // (...)
    });

},
Ze Luis
  • 236
  • 2
  • 15
  • Could you share your models and controller code? – roign Feb 10 '15 at 11:18
  • @RobertIgnat thank you for replying. I've added some relevant code to the question. – Ze Luis Feb 10 '15 at 12:10
  • First of all, Waterline does not support nested population at the moment, so `Company.find().populate('facilities')` will only populate the facilities for each company, but not the warehouses for each facility. Second of all, have you tried removing `references` and `on` in your models? If yes and you get the same behavior, can you show an example of the response? – roign Feb 10 '15 at 16:27
  • @RobertIgnat thank you again. I'll try that and show my results afterwards. – Ze Luis Feb 10 '15 at 16:52
  • @RobertIgnat please post that as an answer and I'll mark it as the accepted answer. It worked, you're the Man!! thanks :) – Ze Luis Feb 10 '15 at 16:57
  • @RobertIgnat by the way, do you know how can I populate my response with the warehouses? – Ze Luis Feb 10 '15 at 17:00

1 Answers1

1

You should remove the references and on from your models.

Regarding nested population, just like I said in the comment, Waterline does not currently support it. You could check Waterline2 which, as they say, offers the possibility of nested populating, but is not recommended for production.
Otherwise you could check this out: Sails.js populate nested associations

Community
  • 1
  • 1
roign
  • 452
  • 5
  • 10
  • In this moment, I cannot switch to waterline2 because my app have an significant amount of code, and will switch to production soon. But I'll check the new Waterline! Thank you! :) – Ze Luis Feb 10 '15 at 18:24