【发布时间】:2021-09-02 20:18:16
【问题描述】:
我不知道该主题是否正确描述了我在这里写的内容。 我有个主意:
我有:
db.transaction(async t => {
await Period.bulkCreate(newPeriods);
const currentPeriods = await Period.findAll({
attributes: ['id', 'firstDay', 'lastDay'],
where: { termId: id }
});
const updatedDate = {
id: dates[j].id,
periodId: currentPeriods[i].dataValues.id
date: dates[j].dataValues.date
};
await Date.update(
updatedDate,
{ where: { period: existedPeriods[i].id } },
{ transaction: t }
);
})
目前,一切正常。但我想将 bulkCreate 放入这样的事务中:
await GradingPeriod.bulkCreate(newPeriods, { transaction: t });
currentPeriods 将是一个空数组。反正有没有使用事务来包装它?也许是这样的
db.transaction(async t => {
db.transaction(async t1 => {
// some other work with Period
await Period.bulkCreate(newPeriods, {transaction: t, transaction: t1});
}).then().catch();
const currentPeriods = await Period.findAll({
attributes: ['id', 'firstDay', 'lastDay'],
where: { termId: id }
});
db.transaction(async t2 => {
const updatedDate = {
id: dates[j].id,
periodId: currentPeriods[i].dataValues.id
date: dates[j].dataValues.date
};
await Date.update(
updatedDate,
{ where: { period: existedPeriods[i].id } },
{ transaction: t, transaction: t2 }
);
}).then().catch();
}).then().catch();
所以每当事务 t1 或 t2 回滚时,整个事务 t 也会回滚。我做了很多研究,但似乎我仍然没有使用正确的关键字进行搜索。所以请帮忙。谢谢。
【问题讨论】:
-
您希望它们包含在一个事务中吗?
-
我试过了,如上所述,我想在做 crud 后使用 currentPeriods,一切都适用于更新查询,但不是 bulkCreate
标签: node.js transactions sequelize.js