From 453a9036be3d121a38d41581c0ca80c6c176b8c5 Mon Sep 17 00:00:00 2001 From: PatrikKozak <35232443+PatrikKozak@users.noreply.github.com> Date: Mon, 30 Jan 2023 16:24:59 -0500 Subject: [PATCH] fix: relation to many index unique fields (#1979) --- src/mongoose/buildSchema.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/mongoose/buildSchema.ts b/src/mongoose/buildSchema.ts index ee3ad1efd3..e30a3810c1 100644 --- a/src/mongoose/buildSchema.ts +++ b/src/mongoose/buildSchema.ts @@ -241,12 +241,16 @@ const fieldToSchemaMap: Record = { let localeSchema: { [key: string]: any } = {}; if (hasManyRelations) { - localeSchema._id = false; - localeSchema.value = { + localeSchema = { + ...formatBaseSchema(field, buildSchemaOptions), type: Schema.Types.Mixed, - refPath: `${field.name}.${locale}.relationTo`, + _id: false, + value: { + type: Schema.Types.Mixed, + refPath: `${field.name}.${locale}.relationTo`, + }, + relationTo: { type: String, enum: field.relationTo }, }; - localeSchema.relationTo = { type: String, enum: field.relationTo }; } else { localeSchema = { ...formatBaseSchema(field, buildSchemaOptions), @@ -263,12 +267,16 @@ const fieldToSchemaMap: Record = { localized: true, }; } else if (hasManyRelations) { - schemaToReturn._id = false; - schemaToReturn.value = { + schemaToReturn = { + ...formatBaseSchema(field, buildSchemaOptions), type: Schema.Types.Mixed, - refPath: `${field.name}.relationTo`, + _id: false, + value: { + type: Schema.Types.Mixed, + refPath: `${field.name}.relationTo`, + }, + relationTo: { type: String, enum: field.relationTo }, }; - schemaToReturn.relationTo = { type: String, enum: field.relationTo }; if (field.hasMany) { schemaToReturn = {