Unable to rollback parent table record if join table update fails

54
July 23, 2022, at 6:00 PM

I've a M:N(many-to-many) relationship between Invoice and Item entities and I'm using sequelize unmanaged transaction to create an invoice by adding items in it.

The problem is, if there are any exceptions thrown while updating the join table(invoice_items) by adding items in it, the sequelize should roll back the parent(invoice) table transaction.

Sequelize should not create an invoice if the join table throws an error while adding items in it.

Look at the code below to understand the scenario,

// start a transaction and save it into a variable
const t = await db.sequelize.transaction();  
try {
   // create new invoice
   invoice = await Invoice.create(invoice, { transaction: t });

  // adding records in join table
  await invoice.addItems([itemId], {
      through: {
          quantity: item.quantity,
          rate: item.rate
      }
  }, { transaction: t });

  await t.commit();
} catch (err) {
  await t.rollback();
}

Rent Charter Buses Company
READ ALSO
Call webpack-ed function from JS file

Call webpack-ed function from JS file

I'm facing an issue, the goal I want to achieve is call a function from a webpack file

82
how do you create a folder and write to heroku's ephemeral storage?

how do you create a folder and write to heroku's ephemeral storage?

i'm using nodejs, and when my script uses fsmkdir nothing seems to happen

53
Is there an additional server configuration for api redirection using nodejs?

Is there an additional server configuration for api redirection using nodejs?

Im currently developing a website for out company that requires connection between two servers via APIThe problem is, on our laptop it redirection works fine but when I deploy it on our test server, it uses its own URL instead of the one we set on the nodejs...

94
How to delete messages EFFICIENTLY with Discord bot

How to delete messages EFFICIENTLY with Discord bot

I want have a Discord Music Bot that when it leaves the voice channel it clears the text channel from commands and logsBut the problem is that it takes too much time (up to 1-3 mins)

122