Sails/Waterline populate does not work in junction table?

843 views Asked by At

Waterline populate gives me an empty collection when I try to populate a junction table (model) with another junction table (model). I'm using many-to-many through relationship as given here:

https://github.com/balderdashy/waterline/issues/705#issuecomment-60945411

I am using sails-mongo adapter for mongolabs.

The example code,

Model user

/user.js

module.exports = {

schema: true,
attributes: {
    id: {
        type: 'string',
        primaryKey: true,
    },
    student: {
        type: 'string'
    },
    vendor: {
        type: 'string'
    },
    courses: {
        collection: 'vendorcourse',
        via: 'users',
        through: 'usercourse'
    },
    coursesProvided: {
        collection: 'course',
        via: 'vendors',
        through: 'vendorcourse'
    }
}
};

Model Course

/course.js
module.exports = {

attributes: {
    id: {
        type: 'string',
        primaryKey: true,
        autoIncrement: true
    },
    name: {
        type: 'string',
        required: true
    },
    description: {
        type: 'string'
    },
    type: {
        type: 'string'
    },
    vendors: {
        collection: 'User',
        via: 'courseProvided',
        through: 'vendorcourse'
    }
}
};

Model vendorcourse

module.exports = {
tableName: 'vendorcourse',
tables: ['user', 'course'],
junctionTable: true,
attributes: {
    id: {
        type: 'string',
        autoIncrement: true,
        primaryKey: true
    },
    name: {
        type: 'string',
        required: true
    },
    course: {
        columnName: 'course',
        type: 'string',
        foreignKey: true,
        references: 'course',
        on: 'id',
        via: 'vendors',
        groupBy: 'course',
        required: true
    },
    vendor: {
        columnName: 'vendor',
        type: 'string',
        foreignKey: true,
        references: 'user',
        on: 'id',
        via: 'coursesProvided',
        groupBy: 'user',
        required: true
    },
    users: {
        collection: 'user',
        via: 'courses',
        through: 'usercourse'
    }
}
};

Model usercourse

/usercourse.js

module.exports = {
tableName: 'usercourse',
tables: ['user', 'vendorcourse'],
junctionTable: true,
attributes: {
    id: {
        type: 'string',
        autoIncrement: true,
        primaryKey: true
    },
    course: {
        columnName: 'course',
        type: 'string',
        foreignKey: true,
        references: 'vendorcourse',
        on: 'id',
        via: 'users',
        groupBy: 'vendorcourse',
        required: true
    },
    user: {
        columnName: 'user',
        type: 'string',
        foreignKey: true,
        references: 'user',
        on: 'id',
        via: 'courses',
        groupBy: 'user',
        required: true
    }
}
};

When I do

usercourse.find().populate('course').exec(function(error,result){
     console.log(result);
});

The output is

[{
   'id': //some ID,
   'course': []  // this is should be populated with the relative record from the vendorcourse model
   'user' : //some ID
}]

The course field should contain the record from the vendorcourse model but instead I get an empty collection [].

The same command works when I do

 usercourse.find().populate('user').exec(function(error,result){
       console.log(result);
  });

The output is as expected

  [{
        'id': //some ID,
         'course': //some ID,
         'user': [{ //relevent data }]
    }] 

Am I doing it wrong? OR Is it that sails/waterline doesnt not support a populate from another junction table

0

There are 0 answers