如果表中没有记录,我需要向表中添加一些记录。现在,我已经编写了在sequelize.sync({force:true})的回调函数中添加记录的逻辑。我检查了该表是否包含至少一条记录,如果没有,我就插入了这组记录。还有其他优雅的方式来获得此功能吗?
最佳答案
您可以使用findOrCreate
Like.findOrCreate({
where: {//object containing fields to found
InstagramPostId: inputLike.InstagramPostId,
InstagramUserId: inputLike.InstagramUserId
},
defaults: {//object containing fields and values to apply
postInstagramPostId: inputLike.postInstagramPostId,
userInstagramUserId: inputLike.userInstagramUserId
}
}).error(function(err){//error handling
console.log(err);
}).then(function(){//run your calllback here
console.log("callback!!");
});
关于node.js - 如果sequelize.sync({force :true}) is called之后不存在,则将记录插入表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32542523/