How to update without select?
See original GitHub issueIssue type:
[x] question [ ] bug report [x] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql
/ mariadb
[ ] oracle
[x] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[x] latest
[ ] @next
[ ] 0.x.x
(or put your version here)
Steps to reproduce or a small repository showing the problem: I don’t know if is a bug or not but when i perform an update it selects the entity again. Is it possible to avoid this second select?
Example code:
let example = await Example.findOne({ id: "ID_HERE" });
if (!example)
throw new Error("Not found");
example.data = 'NEW_DATA';
example.save();
Result Query:
query: SELECT "Example"."id" AS "Example_id", "Example"."data" AS "Example_data" FROM "example" "Example" WHERE "Example"."id" = $1 LIMIT 1 -- PARAMETERS: ["104d691c-37a3-4eac-9185-17cb987d938a"]
query: SELECT "Example"."id" AS "Example_id", "Example"."data" AS "Example_data" FROM "example" "Example" WHERE "Example"."id" IN ($1) -- PARAMETERS: ["104d691c-37a3-4eac-9185-17cb987d938a"]
query: START TRANSACTION
query: UPDATE "example" SET "data" = $2 WHERE "id" IN ($1) -- PARAMETERS: ["104d691c-37a3-4eac-9185-17cb987d938a","EXAMPLE DATA"]
query: COMMIT
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:9 (2 by maintainers)
Top Results From Across the Web
Update a record without first querying? - Stack Overflow
Here is a sample how I am doing it now: dataItem itemToUpdate = (from t in dataEntity.items where t.id == id select t)....
Read more >Entity Framework - Update without Select - GitHub Gist
So what to do. One way would be to detach all of them from the ChangeTracker and then do the state change and...
Read more >How to update entity without fetching it first - MSDN - Microsoft
Hi,. I've requirement to update entity without selecting it first to avoid 2 database hits. I've googled and read a lot on this...
Read more >UPDATE statement with and without SELECT keyword
The FROM is a part of the (correlated) subquery, the SELECT . That is, your UPDATE statement has this form: UPDATE table SET...
Read more >How to UPDATE from a SELECT statement in SQL Server
That solved my problem, but the use of indexes is not clear to me. First you run the UPDATE query without indexes and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
No, if i do not use the
.save()
method it just does one SELECT.I also tried with MySQL, the behavior is the same, it SELECTS before UPDATE, maybe this is a bug, because it should be after the update, and in case of PostgreSQL it should use RETURNING.
Here is an example of the bug; https://github.com/angelvega93/typeorm_bug
And the problem is not only with SAVE, it also happens with REMOVE, it reloads before removing.
I think the problem should be here: https://github.com/typeorm/typeorm/blob/0aa79173e667e193cee653492176a43eb06abcd5/src/persistence/EntityPersistExecutor.ts#L95-L99
I don’t believe this is a bug at all, though I can’t speak for the devs. From what I can tell you can pass a partial object to an EntityManager.save call. IE, just the primary key and maybe a couple fields to update. Internally the framework grabs the current version of the entity from the repository before building an UPDATE so it can compare your properties to the current values in the table. It seems to do this so that it can build a minimum UPDATE statement (only changed fields) or skip it entirely if all values match.
I can see how that would conflict with many people’s expectations of skipping another round trip, but I guess the way of thinking about it is that TypeORM has the opinion that its better to do another round trip for a read if it means skipping getting row level (if InnoDB, or table level if the engine is MyISAM) locks in MySQL, etc. This is semi-reasonable.
I believe the
{reload: false}
is different and is supposed to control whether a fresh version of an object is returned afterwards, and I think (possibly a bug) it is only used on INSERTs. By default if you do an INSERT with a partial object, it has to fully reload the object after save to get various fields that were set by column defaults or triggers. I think reload: false turns this off and you have to reselect the current row yourself if you want it.I think though that there is an issue where this same behavior is not working on updates (IE the reload AFTER the update, not the one that is BEFORE the update) and reload true or false has no effect on that.