I am trying to delete all report_details before bulkCreate/insert the new ones. The problem is when there is an error in bulkCreate it does not rollback. It should bring the destroyed report_details back but it is not working.
The way i am testing this transaction code is I insert the report_details and then manually change one column name so that when inserting again it gives column error. and transaction should rollback but in actual report_details are destroyed and on bulkCreate error it does not bring back destroyed report_details can some one please take a look on my code. I have search on google my syntax is correct. and how do i test transactions on my machine ? instead of changing the column name is there any other way to produce error?
function saveReportsDetails(results) {
db.report_detail.bulkCreate(results.report.objAllReportsDetail);
return db.snpreq.transaction(t => {
// transaction block
return db.report_detail.destroy({
where: {
profile_id: results.profile.data[0].id
}
}, {
transaction: t
}).then(deleted => {
console.log('*******TRANSACTION DELETED*********');
return db.twenreport_detail.bulkCreate(results.report.objAllReportsDetail, {
transaction: t
}).then(reports_created => {
console.log('*******TRANSACTION bulk created*********');
});
});
}).then(transaction => {
console.log('********All Transaction********');
}).catch(err => {
console.log('*******ROLL BACK*********');
});
}
throw new Error()
in the success of TRANSACTION bulk created The code goes incatch
and ROLL BACK is printed but still destroyed report_details does not come back – Keffer