续集:更新模型及其关联

我有以下表格:

  1. Event
  2. Tasks

和下面的关联

 Event.hasMany(models.Tasks, { as: 'tasks', foreignKey: 'event_id' }) 

我正在使用下面的代码来更新一个Event和一个Tasks数组

 db.Event.findOne({ where: { id: eventToUpdate.id }, include: [{ model: db.Tasks, as: 'tasks', }] }).then((existingEvent) => { // Loop through existingEvent attributes and existingEvent.dataValues.tasks and // updated the attributes } 

但是,当我更新它时:

  db.Event.update(existingEvent, { where: { id: existingEvent.id } }) .then((updatedEvent) => { console.log(updatedEvent) }).catch((error) => { console.log("Error + error) }) 

只在数据库中更新Event ,而不是其Tasks

我究竟做错了什么? 还是有更好的方法来完成这个?

很难在评论中更新它。 但这是我build议要做的,

 db.Event.findOne({ where: { id: eventToUpdate.id }, include: [{ model: db.Tasks, as: 'tasks', }] }).then((existingEvent) => { return Promise.all(existingEvent.tasks.map(task => task.updateAttributes(taskParams).then(console.log).catch(console.log)); }