1

I've been struggling for hours to get the simplest populate() query working on my new sails.js project. The problem is that the field to populate always has an empty array. I've pored over the examples, so I don't think I have a syntax error, but maybe my tired eyes aren't letting me see.

Here are the two models + method that's making the query:

Model Department:

    attributes: {
    id: {
        type: 'integer',
        primaryKey: true
    },
    name: {
        type: "string",
        required: true
    },
    shortName: {
        type: "string"
    },
    schoolName: {
        model: 'Schools'
    },
    courses: {
        collection: 'Courses',
        via: "departmentId"
    }
},

Model Courses:

    attributes: {
    id: {
        type: "integer",
        primaryKey: true
    },
    name: {
        type: "string",
        required: true
    },
    shortName: {
        type: "string"
    },
    departmentId: {
        model: "Departments"
    }
},

And finally, the method making the call:

    getDepartmentsBySchool: function(schoolName, callback){
    Departments.find({schoolName: schoolName}).populate("courses").exec(function(error, departments){
        if(error){
            console.log("Departments could not be found");
            return callback(error, []);
        } else{
            console.log(departments.length + " departments found");
            return callback(null, departments);
        }
    });
}

Result:

courses: []

always.

I am using MySQL, so the adapter is sails-mysql. Any sort of pointer would be greatly appreciated so that I don't have to move away from using this framework.

EDIT:

Changing the migration to "alter" results in the following:

!http://tinypic.com/r/9kv9ev/8

Albert
  • 23
  • 2
  • 6
  • Does the answer below solve your problem ? If yes, it is generally considered a good practice and recommended that you accept the correct answer. If not, let me know so that we can resolve it – Mandeep Singh Dec 05 '14 at 18:46
  • Hey mandeep, thanks so much for putting the time into trying it out (I didn't know there was an answer to the question, didn't receive an email notification o.O) Unfortunately, it didn't do the trick, so I'm jumping back into the issue to debug it further. – Albert Dec 10 '14 at 20:03

1 Answers1

0

I tested the code sample provided by you at my end. Instead of keeping shoolName as a reference to another collection, I had kept it as string since I wanted to test the association between departments and courses. Everything seems to work fine. Courses were getting populated perfectly. You might wanna check one thing. While using associations of the type:

   schoolName: {
        model: 'Schools'
    },

the schoolName must contain the id of record from schools table. I hope you are setting that up correctly.

Sometimes it can be an issue related to the node module sails-mysql. You may try reinstalling sails-mysql module

npm uninstall sails-mysql
npm cache clear
npm install sails-mysql

Let me know if this works or not.

Mandeep Singh
  • 7,674
  • 19
  • 62
  • 104
  • I suspected that it might've been the npm installation of sails-mysql, but it didn't affect anything. I have my basic project up on GitHub (master): https://github.com/albertkim/Quaestio The directory 'sql' contains my initialization script. If everything worked on your end, I believe my issue might be how I have my tables set up? – Albert Dec 10 '14 at 20:09
  • @user2651202 You can try one thing. Since its a test project and initial setup, you can keep the migrate settings as "alter" or "drop" for the first time you lift your sails. Once all the tables are created by sails, then revert the settings to "Safe" from next time onwards. Its located in config/models.js – Mandeep Singh Dec 11 '14 at 07:13
  • Hey Mandeep, you may have lead me onto something. I changed the migrate settings to auto, and there's a console error on startup. The relevant line appears to be: "... error in your SQL syntax... near 'SELEC FROM 'courses' AS 'courses' at line 1" Now, there's a typo in that message: SELEC instead of SELECT. – Albert Dec 14 '14 at 02:30
  • Seems to be related to https://groups.google.com/forum/#!topic/sailsjs/D1u-R4cDwxA I'll do some further investigations. – Albert Dec 14 '14 at 02:44
  • I dont think there is any setting named "auto". Try "alter" or "drop" for the first time. Then change it back to "safe". – Mandeep Singh Dec 14 '14 at 05:59
  • Setting it to alter worked after I cleared out the existing tables in the db. Apparently there is some discrepancy between the tables generated by Waterworks and the ones I defined in sql. Thanks for the help. – Albert Dec 15 '14 at 01:46