question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error: Transaction query already complete.

See original GitHub issue

I’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:closed
  • Created 5 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
fl0wcommented, May 23, 2018

You should at least add await knex('tasks').transacting(trx).where({spotId: spot.id}).update('isDeleted', true);

0reactions
MisooBcommented, May 22, 2018

thank you all for your help. I’ve managed to get rid of errors by following:

relatedSpots.forEach(async (
      const updatedSpot = await spot.save({isDeleted: true}, {method:"update", patch: true, transacting: trx});
      await knex('tasks').where({spotId: spot.id}).update('isDeleted', true);
});

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found