From 2e6204c1283666ea830ff2cdadf6fb70e1ac2675 Mon Sep 17 00:00:00 2001 From: coyoteecd Date: Wed, 17 Feb 2021 18:32:37 +0200 Subject: [PATCH] fix: (aurora-data-api) get correct increment primary key for multiple entities inserted Closes: #7385 --- src/driver/aurora-data-api/AuroraDataApiDriver.ts | 6 ++++-- test/github-issues/2131/issue-2131.ts | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/driver/aurora-data-api/AuroraDataApiDriver.ts b/src/driver/aurora-data-api/AuroraDataApiDriver.ts index 0df90bdbd15..20619ffc46d 100644 --- a/src/driver/aurora-data-api/AuroraDataApiDriver.ts +++ b/src/driver/aurora-data-api/AuroraDataApiDriver.ts @@ -655,11 +655,13 @@ export class AuroraDataApiDriver implements Driver { /** * Creates generated map of values generated or returned by database after INSERT query. */ - createGeneratedMap(metadata: EntityMetadata, insertResult: any) { + createGeneratedMap(metadata: EntityMetadata, insertResult: any, entityIndex: number) { const generatedMap = metadata.generatedColumns.reduce((map, generatedColumn) => { let value: any; if (generatedColumn.generationStrategy === "increment" && insertResult.insertId) { - value = insertResult.insertId; + // NOTE: When multiple rows is inserted by a single INSERT statement, + // `insertId` is the value generated for the first inserted row only. + value = insertResult.insertId + entityIndex; // } else if (generatedColumn.generationStrategy === "uuid") { // console.log("getting db value:", generatedColumn.databaseName); // value = generatedColumn.getEntityValue(uuidMap); diff --git a/test/github-issues/2131/issue-2131.ts b/test/github-issues/2131/issue-2131.ts index 6d67a4aaf81..9a5accef4f7 100644 --- a/test/github-issues/2131/issue-2131.ts +++ b/test/github-issues/2131/issue-2131.ts @@ -28,7 +28,7 @@ describe("github issues > #2131 InsertResult return the same primary key", () => async () => (connections = await createTestingConnections({ entities: [__dirname + "/entity/*{.js,.ts}"], - enabledDrivers: ["sqlite", "mysql"], + enabledDrivers: ["sqlite", "mysql", "aurora-data-api"], })) ); beforeEach(() => reloadTestingDatabases(connections));