Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

202
Views
Create and update in the same sequelize transaction

I want to insert a new row and immediately update it, all within the same sequelize transaction, is this possible?

My code so far:

let transaction;
    
try {
    transaction = await dbcontext.sequelize.transaction();
    
    const newRole = await dbcontext.Role.create({
          name: "New Role"
    }, { transaction });
    
    await dbcontext.Role.update(
          {
            name: "New name",
          },
          {
            where: {
              id: newRole.id,
            },
          }, { transaction }
    );
    await transaction.commit();
    console.log("Role commited");
} catch (error) {
    console.log("Rollback in progress");
    if (transaction) {
          await transaction.rollback();
    }
    console.log(error);
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

update has only two parameters so the transaction option should be indicated right next to the where option:

await dbcontext.Role.update(
          {
            name: "New name",
          },
          {
            where: {
              id: newRole.id,
            },
            transaction,
          }
    );
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!