我使用sequelize for node.js.我定义了这种关系:
// 1:M - Platform table with Governance status table
dbSchema.Platform_governance.belongsTo(dbSchema.Governance_status, {foreignKey: 'platform_status'});
dbSchema.Governance_status.hasMany(dbSchema.Platform_governance, {foreignKey: 'platform_status'});
这意味着我有一个名为platform_governance的表,它有一个指向governance_status行的外键。用户想要更改此外键以指向不同的governance_status行。
所以我想首先搜索用户选择的这个governance_status行实际存在并且是唯一的,然后使外键指向它。这就是我目前所拥有的:
// first I select the platform_governance of which I want to change it's foreign key
dbSchema.Platform_governance.findById(5, {include: [dbSchema.Governance_status]}).then(result => {
// Now I search for the user-requested governance_status
dbSchema.Governance_status.findAll({where:{user_input}}).then(answer => {
// I check that one and only one row was found:
if (answer.length != 1 ) {
console.log('error')
} else {
// Here I want to update the foreign key
// I want and need to do it through the associated model, not the foreign key name
result.set('governance_status', answer[0])
result.save().then(result => console.log(result.get({plain:true}))).catch(err => console.log(err))
}
result.save()promise成功返回,控制台中打印的对象正确,并且正确设置了新的governance_status。但如果我去数据库,NOTHING已经改变了。什么都没有真正得救。
答案 0 :(得分:0)
// Update corresponding foreign keys
result.setGovernance_status(answer[0])
如果有人在文档中的任何地方找到了记录,我将不胜感激:)