egg插件sequelize:事务
用户表,id自增主键:
user
id
username
create_time
1
admin
1594562489
用户信息表,id为主键:
user_info
id
mobile
1
18888888888
admin@168.com
添加数据:
const userData = { username: "qqzhxl",
create_time: 1594562500
};
const userInfoData = {
email: "qqzhxl@126.com",
mobile: "16666666666"
};
const t = await this.app.model.transaction();
try {
const userRes = await this.ctx.model.User.create(userData, {
raw: true,
transaction: t
});
userInfoData.id = userRes.id;
const userInfoRes = await this.ctx.model.Userinfo.create(userInfoData, {
raw: true,
transaction: t
});
if (userRes && userInfoRes) {
t.commit();
return true;
}
t.rollback();
} catch (e) {
t.rollback();
}
注意: 如果出现userRes.id为空,则在user模型定义时设置主键的autoIncrement为true,未设置的话返回的自增id会存在userRes.null里
以上是 egg插件sequelize:事务 的全部内容, 来源链接: utcz.com/z/518277.html