0

I have two collections in mongodb database and model for each of them

App Model

module.exports = {

    tableName: 'app',
    attributes: {
        _id : {
            primaryKey: true,
            unique: true,
            type: 'string',
        },
        userId: {
            model: 'user'
        },
        title: {
            type: 'string',
            required: true,
            unique: true,
        },
        createdDate : 'string'
    },
};

and User Model

module.exports = {

    tableName: 'user',
    attributes: {
        id : {
            primaryKey: true,
            unique: true,
            type: 'string',
            collection: "app",
            via : "userId"
        },
        password: {
            type: 'string',
            required: true
        },
        apps : {
            collection: "app",
            via : "userId"
        }
    },

};

When i use numeric values for join this collection, it works fine, but when i try do it with mongodb native id object, i get the empty result

How i call join query

User.find().populate('apps').exec(function(err, result) {});
styopdev
  • 2,604
  • 4
  • 34
  • 55

1 Answers1

0

You need to get rid of both the _id and id attribute definitions in your models. Waterline will handle the primary key fields for you automatically (normalizing them to id), so unless you need to change the field type, they can be safely left out. Also, I'm not sure what your intention was by adding collection and via to the id definition, but the primary key is never going to be an association.

Otherwise, your models look correct. If you get rid of those two attributes, things should work fine.

sgress454
  • 24,870
  • 4
  • 74
  • 92