在同一个sequelize事务中创建和更新
我想插入一个新行并立即更新它,所有这些都在同一个续集事务中,这可能吗?
到目前为止我的代码:
let transaction;
try {
transaction = await dbcontext.sequelize.transaction();
const newRole = await dbcontext.Role.create({
name: "New Role"
}, { transaction });
await dbcontext.Role.update(
{
name: "New name",
},
{
where: {
id: newRole.id,
},
}, { transaction }
);
await transaction.commit();
console.log("Role commited");
} catch (error) {
console.log("Rollback in progress");
if (transaction) {
await transaction.rollback();
}
console.log(error);
}
I want to insert a new row and immediately update it, all within the same sequelize transaction, is this possible?
My code so far:
let transaction;
try {
transaction = await dbcontext.sequelize.transaction();
const newRole = await dbcontext.Role.create({
name: "New Role"
}, { transaction });
await dbcontext.Role.update(
{
name: "New name",
},
{
where: {
id: newRole.id,
},
}, { transaction }
);
await transaction.commit();
console.log("Role commited");
} catch (error) {
console.log("Rollback in progress");
if (transaction) {
await transaction.rollback();
}
console.log(error);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
update
只有两个参数,因此transaction
选项应指示在where
选项旁边:update
has only two parameters so thetransaction
option should be indicated right next to thewhere
option: