Error: Transaction query already complete.
See original GitHub issueI’ve been trying to achieve this code to work for days now, though unsuccessfully.
So I have model of an “Object”, which has many “Spots” and those have many “Tasks” (both are working hasMany relations).
What I am trying to do is when I soft delete an Object (just passing a flag isDeleted: true), I also need to pass this flag to all of it’s Spots and to all of it’s Tasks.
const updatedObjekt = await Bookshelf.transaction( async (trx) => {
// if deleting object
if (req.body.isDeleted) {
const relatedSpots = await new Spot().where({objektId: objekt.attributes.id}).fetchAll();
//soft delete spots
relatedSpots.forEach(async (spot) => {
const relatedTasks = await new Task().where({spotId: spot.attributes.id}).fetchAll();
//soft delete tasks
relatedTasks.forEach(async (task) => {
const updatedTask = await task.save({isDeleted: true}, {method:"update", patch: true, transacting: trx});
});
const updatedSpot = await spot.save({isDeleted: true}, {method:"update", patch: true, transacting: trx});
});
}
//save objekt
const updatedObjekt = await objekt.save(req.body, {method:"update", patch: true, transacting: trx, returning: '*'});
if (managerId) {
await updatedObjekt.managedBy().detach();
await updatedObjekt.managedBy().attach(managerId);
}
return updatedObjekt;
});
I end up with this error: UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: Transaction query already complete.
Is there any way to achieve this please?
Issue Analytics
- State:
- Created 5 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
knex.js - Unhanded rejection error :Transaction query already ...
Throwing an error directly from the transaction handler function automatically rolls back the transaction, same as returning a rejected ...
Read more >Error: Transaction query already complete. #2621 - GitHub
I've been trying to achieve this code to work for days now, though unsuccessfully. So I have model of an "Object", which has...
Read more >Vincit/objection.js - Gitter
but I am unable to fetch required data using following query: ... Error: Transaction query already complete, run with DEBUG=knex:tx for more info ......
Read more >Transactions | Knex.js
All queries within a transaction are executed on the same database connection, and run the entire set of queries as a single unit...
Read more >Mastering transactions with Knex.js and Objection.js
const users = await query. Knex.js also manages transactions thanks to a transaction method: async function createUserAndMovie() {.
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 Free
Top 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
You should at least add
await knex('tasks').transacting(trx).where({spotId: spot.id}).update('isDeleted', true);
thank you all for your help. I’ve managed to get rid of errors by following:
first line adds ‘isDeleted’ flag to all spots and second one does the same with tasks. Don’t know if this is super viable or not but it’s working for now.