From cacfd33ef6e69977f5eaf5b7445a2ffc669f3aa3 Mon Sep 17 00:00:00 2001 From: James Ward Date: Tue, 21 Jul 2020 13:31:49 -0400 Subject: [PATCH] fix: prevent wrong returned entity in ReturningResultsEntityUpdator 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 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/query-builder/ReturningResultsEntityUpdator.ts b/src/query-builder/ReturningResultsEntityUpdator.ts index 7dcd9f100da..b3afbfab20d 100644 --- a/src/query-builder/ReturningResultsEntityUpdator.ts +++ b/src/query-builder/ReturningResultsEntityUpdator.ts @@ -120,6 +120,12 @@ export class ReturningResultsEntityUpdator { await Promise.all(entities.map(async (entity, entityIndex) => { 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 return inserted entity because entity id is not set in the entity.`); + // to select just inserted entity we need a criteria to select by. // for newly inserted entities in drivers which do not support returning statement // row identifier can only be an increment column