I have projects and users.
A user can have many projects.
A project can have multiple users.
I tried to model this with a belongsToMany association.
On my server I defined the associations like that:
user.belongsToMany(project, {
through: 'writer_of_project'
foreign-key: 'user'
as: \projects
});
project.bbelongsToMany(user, {
through: 'writer_of_project'
foreign-key: 'project'
as: 'writers'
});
On my client it looks like this:
user: {
id: 1,
...
projects: [1,2,3]
}
project: {
id: 1,
...
writers: [1,4,5]
}
On the server the association requires a third table to store the association and Sequelize doesn't seem to let me include the corresponding models from it.
If I run a project.find(1)
with include:[user]
I get
user is not associated with project!
If I try to put the project from the example above into the update method. The users attribute is simply ignored (I expected a project.setUsers(projectUpdate.users to happen in the background).
What is the right way to deal with the loading and updating of these associations?