Reputation: 2287
I have 2 models with a Many-to-Many association. My code to associate these two models is working well (it create a new collection item_languages__language_items
with the corresponding documents inside). But then I have trouble to get all the associated data (languages) of a specific item. I'm using MongoDB.
// Item.js
module.exports = {
schema: true,
autoPK: false,
attributes: {
uuid: {
type: 'string',
primaryKey: true,
unique: true,
required: true,
uuidv4: true
},
languages: {
collection: 'language',
via: 'items',
dominant: true
}
}
}
// Language.js
module.exports = {
schema: true,
autoPK: false,
attributes: {
code: {
type: 'string',
primaryKey: true,
required: true,
minLength: 2,
maxLength: 2,
unique: true
},
items: {
collection: 'item',
via: 'languages'
}
}
}
Data stored in the item_languages__language_items
collection:
/* 0 */
{
"language_items" : "es",
"item_languages" : "69e4f3a3-1247-4a06-ae2d-9df27ac9495b",
"_id" : ObjectId("5330bcebf8e0b61509c771d5")
}
/* 1 */
{
"language_items" : "fr",
"item_languages" : "69e4f3a3-1247-4a06-ae2d-9df27ac9495b",
"_id" : ObjectId("5330bd26f8e0b61509c771d6")
}
/* 2 */
{
"language_items" : "en",
"item_languages" : "69e4f3a3-1247-4a06-ae2d-9df27ac9495b",
"_id" : ObjectId("5330bedcc076355b09da3ccd")
}
Now in my ItemController.js
, I want to get a specific item with all associated languages:
Item
.findOne({uuid: '69e4f3a3-1247-4a06-ae2d-9df27ac9495b'})
.populate('languages')
.exec(function (e, r) {
console.log(r.toJSON());
});
But here I get my item with only 1 associated language, when I expected to get the 3 associated languages.
Upvotes: 2
Views: 446
Reputation: 24948
This appears to be a bug in the current beta implementation of sails-mongo
which keeps populate
from working properly with custom-defined keys. Please post this to the sails-mongo issues forum! In the meantime the only solution appears to be to use the default MongoDB primary keys.
Upvotes: 2