From c1c8e88f8945bf6a03bde728de370f5c61c5bdb8 Mon Sep 17 00:00:00 2001 From: James Ward Date: Tue, 29 Sep 2020 12:39:37 -0400 Subject: [PATCH] fix: prevent wrong returned entity in ReturningResultsEntityUpdator (#6440) in the `ReturningResultsEntityUpdator` we have to check that the `entityId` we pull from the entity is actually there. if we don't we will create a where clause that's undefined - effectively querying for every record in the database and returning the wrong value --- src/query-builder/ReturningResultsEntityUpdator.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/query-builder/ReturningResultsEntityUpdator.ts b/src/query-builder/ReturningResultsEntityUpdator.ts index 2080dcf366..c669dd9c10 100644 --- a/src/query-builder/ReturningResultsEntityUpdator.ts +++ b/src/query-builder/ReturningResultsEntityUpdator.ts @@ -119,6 +119,13 @@ export class ReturningResultsEntityUpdator { if (this.queryRunner.connection.driver.isReturningSqlSupported() === false && insertionColumns.length > 0) { const entityIds = entities.map((entity) => { const entityId = metadata.getEntityIdMap(entity)!; + + // We have to check for an empty `entityId` - if we don't, the query against the database + // effectively drops the `where` clause entirely and the first record will be returned - + // not what we want at all. + if (!entityId) + throw new Error(`Cannot update entity because entity id is not set in the entity.`); + return entityId; });