gpt4 book ai didi

node.js - 如果 Upsert 是更新,如何撤消更新;如果 Upsert 是使用 Sequelize 插入,则如何删除

转载 作者:太空宇宙 更新时间:2023-11-03 23:14:24 26 4
gpt4 key购买 nike

我正在尝试使用 Sequelize 添加将数据创建到我的expressJs 应用程序中的 2 个表中的功能。

Controller :
Campaign.upsert({
institution_id : institution_id,
description : campaign_about
}).then(value => {
InstitutionDetail.create({
institution_id : institution_id,
support_description : support_description,
img_homepage : img_homepage
}).then(value1 => {
var campaign = {};
var obj = {campaign};

obj.campaign = value;
obj.institution_detail = support_description;
obj.institution_img_home = img_homepage;

res.json({
'data': obj
})
}).catch(reason => {
if(value == true){
//if campaign created
Campaign.destroy({force: true, where:{institution_id:institution_id}});
} else {
//if campaign updated
//how to undo the updated, so I can have the previous data back
Campaign.destroy({where:{institution_id:institution_id}});
}
News.destroy({where:{id:value2.id}});
Testimony.destroy({where:{id:value1.id}});
responseUtil.fail(res, reason)
})
}).catch(reason => responseUtil.fail(res, reason))

如果InstitutionDetail中的create失败,我想销毁Campaign创建的数据,或者取回之前的数据,如果营销事件进行更新

如果Campaign执行create,我已在catch中使用destroy成功删除了Campaign数据,但我不知道如果广告系列进行更新该怎么办。

我尝试在模型中使用paranoid:

timestamps: true,
paranoid: true

并在模型中添加列deletedAt

但它仅将deletedAt设置为当前时间,如果Campaign执行更新,则不会取回数据

最佳答案

As per the doc :

Returns a boolean indicating whether the row was created or updated. For MySQL/MariaDB, it returns true when inserted and false when updated. For Postgres/MSSQL with (options.returning=true), it returns record and created boolean with signature .

所以尝试一下:

Campaign.upsert({
institution_id : institution_id,
description : campaign_about
}).then(value => {
console.log(value); // <--- This should return true/false
});


Campaign.upsert({
institution_id : institution_id,
description : campaign_about
},{ returning : true }) // <----- with (options.returning=true)
.then(value => {
console.log(value); // <--- This will return model + created : true/false
});

关于node.js - 如果 Upsert 是更新,如何撤消更新;如果 Upsert 是使用 Sequelize 插入,则如何删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57407621/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com