我尝试将这两个查询包装在这样的事务上
const transaction = await db.sequelize.transaction()
try {
await Table1.create({
name: data.name
}, {transaction});
trainees.foreach(async trainee => {
await Table2.create({
name: trainee.name
}, {transaction});
})
await transaction.commit();
api.publish(source, target, false, {message: `Data successfully saved`});
} catch (error) {
await transaction.rollback();
api.error(source, target, {
message: error.message || `Unable to save data`
});
}
Run Code Online (Sandbox Code Playgroud)
执行第一个查询,但第二个查询出现以下错误。
commit has been called on this transaction(2f8905df-94b9-455b-a565-803e327e98e1), you can no longer use it. (The rejected query is attached as the 'sql' property of this error)
Run Code Online (Sandbox Code Playgroud)