diff --git a/packages/strapi-generate-api/templates/mongoose/service.template b/packages/strapi-generate-api/templates/mongoose/service.template index cf726312bf..5b718c9940 100755 --- a/packages/strapi-generate-api/templates/mongoose/service.template +++ b/packages/strapi-generate-api/templates/mongoose/service.template @@ -48,8 +48,10 @@ module.exports = { * @return {Promise} */ - add: (values) => { - return <%= globalID %>.create(values); + add: async (values) => { + const data = await <%= globalID %>.create(_.omit(values, _.keys(_.groupBy(strapi.models.<%= id %>.associations, 'alias')))); + await strapi.hook.mongoose.load().manageRelations(strapi.models, <%= globalID %>, _.merge(_.clone(data), { values })); + return data; }, /** diff --git a/packages/strapi-mongoose/lib/index.js b/packages/strapi-mongoose/lib/index.js index 5db41bbd20..23b1df5865 100755 --- a/packages/strapi-mongoose/lib/index.js +++ b/packages/strapi-mongoose/lib/index.js @@ -369,8 +369,8 @@ module.exports = function (strapi) { if (response[current] && _.isObject(response[current]) && response[current][Model.primaryKey] !== value[current]) { virtualFields.push( - this.manageRelations(models, Model, { - id: response[current][Model.primaryKey], + this.manageRelations(models, models[details.model || details.collection], { + _id: response[current][Model.primaryKey], values: { [details.via]: null },