You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[ ] latest
[ ] @next
[ ] 0.x.x (or put your version here)
I have a routine where I perform various loop inserts and updates. It is too heavy due to entity's .save () performing a select on dependencies after insert / update.
Would you have any parameters to send that would run .save () without select later?
Its that my entity will be outdated, but it would get faster.
Reacted by Juan Sebastián Bedoya Arévalo and martin-scholz-emnify
I'm facing a similar issue. Doing a save() with reload: false is the only way to do insert/update only, but the problem there is that you don't have access to the inserted row ID (for insert) or the number of matched and affected rows (for update), and I need the insert ID.
@imnotjames@pleerock We have the same issue with the SELECT after INSERT which has been discussed many times over different issues. We are using Aurora Data API driver with mysql.
I do not mind overriding insert/update in our custom repositories to use updateEntity(false). What is troubling in this case is that the inserted IDs are not set back into the entity. You might say, well, that's what updateEntity(false) says it does. However, the consequence is that there is really no way you can run a "fast and efficient INSERT query" with TypeORM, other than going down to raw "query" level.
Aurora Data API driver (and likely mysql as well) returns the insertId in the raw insert result. It's just that TypeORM decides to throw it away because entity update is false.
Looking at the code, InsertQueryBuilder uses ReturningResultsEntityUpdator to process the raw insert result and generate the identifiers map. This is NOT done if updateEntity is false:
But if I leave updateEntity true, then since MySQL and Aurora Data API drivers do not support RETURNING queries, I get the unnecessary select statement after each insert :(. This sucks. Basically what I need is to run ReturningResultsEntityUpdator.insert without doing the additional query, so omitting this code:
.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
I checked and the generatedMap is filled correctly based on the raw result. A quick hack would be to modify the AuroraDataApiDriver to return true on isReturningSqlSupported(), but this flag also allows you to specify .returning() etc. which is not correct.
Proposal
(optional) introduce an EntityUpdateMode param to updateEntity, so we can do:
Possible values would be None (false mapped to it) and All (true mapped to it). This is necessary to maintain backwards compatibility. Maybe it helps with clarity as well.
modify InsertQueryBuilder so it calls ReturningResultsEntityUpdator.insert when update mode != None, passing the update mode to the insert call. The updator would then only execute the additional SELECT when full update is requested.
This way, the InsertResult gets populated with the correct identifiers array and the entity ID is updated as well.
Note: I am willing to submit a PR with the above changes, in fact I'll probably do them anyway in a fork because it's blocking us. Would love some feedback to avoid wasting time though.
TypeORM has returned to active development after recently transitioning to new leadership (see related announcement).
As part of the new team's efforts to prioritise work on the project moving forward, it is necessary to clean up the backlog of stale issues.
🧹 This issue is being automatically closed because it has had no activity in the last 6 months.
If you believe this issue is still relevant, feel free to comment below and the maintainers may reopen it. Thank you for your contribution.
Activity
deemaagog commentedon Jan 15, 2020
Why not just use update/insert methods instead of save?
meuevento commentedon Jan 15, 2020
they also execute select after insert
boenrobot commentedon Jan 19, 2020
I'm facing a similar issue. Doing a save() with reload: false is the only way to do insert/update only, but the problem there is that you don't have access to the inserted row ID (for insert) or the number of matched and affected rows (for update), and I need the insert ID.
ochanje210 commentedon Jan 28, 2020
having the same issue regarding performance..
coyoteecd commentedon Feb 16, 2021
This question has been answered here: #2870 (comment)
@imnotjames @pleerock We have the same issue with the SELECT after INSERT which has been discussed many times over different issues. We are using Aurora Data API driver with mysql.
I do not mind overriding insert/update in our custom repositories to use
updateEntity(false). What is troubling in this case is that the inserted IDs are not set back into the entity. You might say, well, that's whatupdateEntity(false)says it does. However, the consequence is that there is really no way you can run a "fast and efficient INSERT query" with TypeORM, other than going down to raw "query" level.Aurora Data API driver (and likely mysql as well) returns the insertId in the raw insert result. It's just that TypeORM decides to throw it away because entity update is false.
Looking at the code, InsertQueryBuilder uses ReturningResultsEntityUpdator to process the raw insert result and generate the identifiers map. This is NOT done if updateEntity is false:
typeorm/src/query-builder/InsertQueryBuilder.ts
Lines 113 to 118 in fde9f07
But if I leave updateEntity true, then since MySQL and Aurora Data API drivers do not support RETURNING queries, I get the unnecessary select statement after each insert :(. This sucks. Basically what I need is to run ReturningResultsEntityUpdator.insert without doing the additional query, so omitting this code:
typeorm/src/query-builder/ReturningResultsEntityUpdator.ts
Lines 117 to 150 in fde9f07
I checked and the generatedMap is filled correctly based on the raw result. A quick hack would be to modify the AuroraDataApiDriver to return true on
isReturningSqlSupported(), but this flag also allows you to specify .returning() etc. which is not correct.Proposal
EntityUpdateModeparam to updateEntity, so we can do:Possible values would be None (false mapped to it) and All (true mapped to it). This is necessary to maintain backwards compatibility. Maybe it helps with clarity as well.
This way, the InsertResult gets populated with the correct identifiers array and the entity ID is updated as well.
Note: I am willing to submit a PR with the above changes, in fact I'll probably do them anyway in a fork because it's blocking us. Would love some feedback to avoid wasting time though.
Edit: you can view an example implementation here: https://github.com/coyoteecd/typeorm/commits/fix-aurora-data-api-select-after-insert
github-actions commentedon Sep 18, 2025
TypeORM has returned to active development after recently transitioning to new leadership (see related announcement).
As part of the new team's efforts to prioritise work on the project moving forward, it is necessary to clean up the backlog of stale issues.
🧹 This issue is being automatically closed because it has had no activity in the last 6 months.
If you believe this issue is still relevant, feel free to comment below and the maintainers may reopen it. Thank you for your contribution.