5

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*********');
    });
}
Syed Danial
  • 635
  • 1
  • 6
  • 16
  • If i add `throw new Error()` in the success of TRANSACTION bulk created The code goes in `catch` and ROLL BACK is printed but still destroyed report_details does not come back – Syed Danial Jan 29 '18 at 09:34

1 Answers1

11

There was error in m code syntax. In delete transaction it takes transaction: t in single argument like this

return db.twentythree_and_me_report_detail.destroy({
            where: {
                profile_id: results.profile.data[0].id
            },
            transaction: t
        })

I was not receiving any kind of syntax error or any other error. so, i just kept search and found answer

Syed Danial
  • 635
  • 1
  • 6
  • 16