Skip to content

Commit

Permalink
fix: re-select inserted default and generated values with a single SE…
Browse files Browse the repository at this point in the history
…LECT

closes typeorm#6266
  • Loading branch information
dolsup committed Sep 4, 2020
1 parent cf3ad62 commit 688e070
Showing 1 changed file with 22 additions and 19 deletions.
41 changes: 22 additions & 19 deletions src/query-builder/ReturningResultsEntityUpdator.ts
Expand Up @@ -117,26 +117,29 @@ export class ReturningResultsEntityUpdator {
// for postgres and mssql we use returning/output statement to get values of inserted default and generated values
// for other drivers we have to re-select this data from the database
if (this.queryRunner.connection.driver.isReturningSqlSupported() === false && insertionColumns.length > 0) {
await Promise.all(entities.map(async (entity, entityIndex) => {
const entityIds = entities.map((entity) => {
const entityId = metadata.getEntityIdMap(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
// (since its the only thing that can be generated by those databases)
// or (and) other primary key which is defined by a user and inserted value has it

const returningResult: any = await this.queryRunner.manager
.createQueryBuilder()
.select(metadata.primaryColumns.map(column => metadata.targetName + "." + column.propertyPath))
.addSelect(insertionColumns.map(column => metadata.targetName + "." + column.propertyPath))
.from(metadata.target, metadata.targetName)
.where(entityId)
.setOption("create-pojo") // use POJO because created object can contain default values, e.g. property = null and those properties maight be overridden by merge process
.getOne();

this.queryRunner.manager.merge(metadata.target as any, generatedMaps[entityIndex], returningResult);
}));
return entityId;
});

// to select just inserted entities 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
// (since its the only thing that can be generated by those databases)
// or (and) other primary key which is defined by a user and inserted value has it

const returningResult: any = await this.queryRunner.manager
.createQueryBuilder()
.select(metadata.primaryColumns.map(column => metadata.targetName + "." + column.propertyPath))
.addSelect(insertionColumns.map(column => metadata.targetName + "." + column.propertyPath))
.from(metadata.target, metadata.targetName)
.where(entityIds)
.setOption("create-pojo") // use POJO because created object can contain default values, e.g. property = null and those properties maight be overridden by merge process
.getMany();

entities.forEach((entity, entityIndex) => {
this.queryRunner.manager.merge(metadata.target as any, generatedMaps[entityIndex], returningResult[entityIndex]);
})
}

entities.forEach((entity, entityIndex) => {
Expand Down

0 comments on commit 688e070

Please sign in to comment.