diff --git a/packages/twenty-server/src/engine/twenty-orm/repository/workspace-update-query-builder.ts b/packages/twenty-server/src/engine/twenty-orm/repository/workspace-update-query-builder.ts index 3052ac897a..c5768dd006 100644 --- a/packages/twenty-server/src/engine/twenty-orm/repository/workspace-update-query-builder.ts +++ b/packages/twenty-server/src/engine/twenty-orm/repository/workspace-update-query-builder.ts @@ -235,38 +235,42 @@ export class WorkspaceUpdateQueryBuilder< const results: UpdateResult[] = []; + const nestedRelationQueryBuilder = new WorkspaceSelectQueryBuilder( + this as unknown as WorkspaceSelectQueryBuilder, + this.objectRecordsPermissions, + this.internalContext, + this.shouldBypassPermissionChecks, + this.authContext, + ); + + this.relationNestedConfig = + this.relationNestedQueries.prepareNestedRelationQueries( + this.manyInputs.map( + (input) => input.partialEntity, + ) as QueryDeepPartialEntityWithNestedRelationFields[], + mainAliasTarget, + ); + + if (isDefined(this.relationNestedConfig)) { + const updatedValues = + await this.relationNestedQueries.processRelationNestedQueries({ + entities: this.manyInputs.map((input) => input.partialEntity) as + | QueryDeepPartialEntityWithNestedRelationFields + | QueryDeepPartialEntityWithNestedRelationFields[], + relationNestedConfig: this.relationNestedConfig, + queryBuilder: nestedRelationQueryBuilder, + }); + + this.manyInputs = updatedValues.map((updatedValue, index) => ({ + criteria: this.manyInputs[index].criteria, + partialEntity: updatedValue, + })); + } + for (const input of this.manyInputs) { this.expressionMap.valuesSet = input.partialEntity; this.where({ id: input.criteria }); - const nestedRelationQueryBuilder = new WorkspaceSelectQueryBuilder( - this as unknown as WorkspaceSelectQueryBuilder, - this.objectRecordsPermissions, - this.internalContext, - this.shouldBypassPermissionChecks, - this.authContext, - ); - - this.relationNestedConfig = - this.relationNestedQueries.prepareNestedRelationQueries( - input.partialEntity as QueryDeepPartialEntityWithNestedRelationFields, - mainAliasTarget, - ); - - if (isDefined(this.relationNestedConfig)) { - const updatedValues = - await this.relationNestedQueries.processRelationNestedQueries({ - entities: input.partialEntity as - | QueryDeepPartialEntityWithNestedRelationFields - | QueryDeepPartialEntityWithNestedRelationFields[], - relationNestedConfig: this.relationNestedConfig, - queryBuilder: nestedRelationQueryBuilder, - }); - - this.expressionMap.valuesSet = - updatedValues.length === 1 ? updatedValues[0] : updatedValues; - } - const result = await super.execute(); results.push(result);