fix(relations): avoid duplicate entries in manyToMany relations

This commit is contained in:
g-div 2018-10-29 09:41:23 +01:00
parent 8e393492d2
commit cca35fe9ac

View File

@ -124,22 +124,11 @@ module.exports = {
}
virtualFields.push(
model.findOne({
[model.primaryKey]: getValuePrimaryKey(value, model.primaryKey)
module.exports.addRelation.call(model, {
id: getValuePrimaryKey(value, this.primaryKey),
values: _.pick(value, [this.primaryKey, details.via]),
foreignKey: current
})
.populate([details.via])
.lean()
.then(entry => {
if (_.isArray(entry[details.via])) {
value[details.via] = entry[details.via].map(entry => _.get(entry, '_id', entry)).concat(value[details.via]);
}
return module.exports.addRelation.call(model, {
id: getValuePrimaryKey(value, this.primaryKey),
values: _.pick(value, [this.primaryKey, details.via]),
foreignKey: current
});
})
);
});