To your question:
When you eagerly load FilteredContent (using include), a model instance is already built, so there is no reason to call build
. Something along the lines of this should do what you want:
Filter.find({
where: { id: 3 },
include: [ { model : FilteredContent, as : 'filteredContent' } ]
}).then ( function( filter ) {
return filter.filteredContent[0].updateAttributes({
content: 'crap'
})
}).then(function () {
// DONE! :)
});
A couple of pointers about the code you posted as a whole:
- sequelize.sync creates database tables for your models, if they don't already exist. It is not required for what you want to do if your tables already exist
- sequelize.sync is an async operation, so doing sequelize.sync without attaching a callback is not advisable. Furthermore it looks like you are doing sync in a model definition - you should only do it once, preferrably in the place where you define your models.
- It looks like you defining several models in one file - you should only define one in each file. The associations could be set up by doing sequelize.import([path to filter model) in your FilterContent file, or by doing all associations in the place you import your models into your app.
edit to answer your comment:
You cannot do a single function call that will update both filter and filteredcontent, but you don't have to do the updates in sequence either. You can issue all the update commands without waiting for them to complete.
Filter.find({
where: { id: 3 },
include: [ { model : FilteredContent, as : 'filteredContent' } ]
}).then ( function( filter ) {
return Promise.all([
filter.updateAttributes({}),
filter.filteredContent.map(fc => fc.updateAttributes({}))
]);
}).spread(function (filter, filteredContents) {
})
In this way all queries will run in parallel, and your then function will be called when all of them have completed. Notice that I've used spread
here to turn the array returned from Promise.all
into separate arguments.