I am creating a project using express.js where I want to group one or more database queries performed using sequelze.js and PostgreSQL to create a transaction. I have created managed transaction using sequelize, When one of the query failed subsequent queries didn’t perform but the query before that got committed. The console has logged rollback, but rollback didn’t work.
Is this the usual transaction behavior in sequelize? Is there any configuration to be done to allow rollback? Please find the code below
async function(req){
//some code
try{
const result = await db.sequelize.transaction(async(t)=>{
await Manager.create({
//user data
}, {transaction:t}
)
await Project.create({
//user_secret data
}, {transaction:t})
await Team.create({
//employer data
}, {transaction:t})
//code to generate token based on query data
//return token
});
return result
}catch((err)=>{
console.log("error while performing transaction: "+err);
return err;
}
}
I would like to add that these tables are not related by any relation.
I want all queries to be rolled back, if any operation in transaction block fails. But using this code structure Manager is inserted in DB, even though project insertion failed because of sequelizeerror.
Zainab Firdous is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.