事务中有update
查询。我得到插入或更新表" comp_submitted "违反外键约束"comp_submissions_comp_id_fkey">看起来comp还没有添加到数据库表中。但奇怪的是,CompAddressRel
模型实际上是用comp.id
值
await sequelize.transaction(async (t) => {
const comp = await Comp.create({
...req.body,
submitted_by_id: req.user.id,
is_files_uploaded: false,
publishedAt: req.body.is_public ? new Date() : null,
status: APPROVED
}, { transaction: t })
// This works
await CompAddressRel.create({
comp_id: comp.id,
address_detail_pid: address.address_detail_pid,
building_id: req.body.building_id,
land_id: req.body.land_id,
campaign_id: req.body.campaign_id,
owner_id: req.body.owner_id,
planning_permit_id: req.body.planning_permit_id
}, { transaction: t })
// This doesn't work
await CompSubmission.update({
status: APPROVED,
comp_id: comp.id
}, {
where: {
id: req.body.submission_id
}
}, { transaction: t })
})
CompSubmission
和CompAddressRel
都有comp_id作为外键。只有CompSubmission
不工作
我使用promise使其工作,参见下面的代码。
async createUser(user: UserDTO) {
let usr;
await db.sequelize.transaction().then(async t => {
return await Organizations.createOrganization(
{
name: user.organizationName,
type: user.organizationType
},
{ ...t }
)
.then(async organization => {
usr = await userRepo.create(
{
...user,
// @ts-ignore
Organization: organization.id,
// @ts-ignore
organizationName: org.organizationName,
// @ts-ignore
organizationType: org.organizationType
},
{ ...t }
);
return usr
})
.then(t.commit.bind(t))
.catch(t.rollback.bind(t));
});
return usr
}