Delete records in n:m created models

610 views
Skip to first unread message

Nicola Furnari

unread,
Nov 5, 2018, 10:29:16 AM11/5/18
to Sequelize
hi,
I have a belongs-to-many association User - Projects so, using "through" parameter I created the table worker_tasks in this way

User.belongsToMany(Project, { as: 'Tasks', through: 'worker_tasks', foreignKey: 'userId' }) Project.belongsToMany(User, { as: 'Workers', through: 'worker_tasks', foreignKey: 'projectId' })

It works. I created some Projects then some Users then I associate some User with some projects.

Now I need to delete, for example, the "Project A" and I want also delete those records in worker_task tabel that have ID of the "Project A" as part of the key (the other part is the user ID)
How can I do this?

thanks

Azamat

unread,
Mar 5, 2020, 2:21:46 PM3/5/20
to Sequelize
Hi, 
When you define associations, sequelize defines some special methods that are intended to use for cases such as yours. 
Check out this link from the docs:
https://sequelize.org/master/manual/assocs.html#special-methods-mixins-added-to-instances

понедельник, 5 ноября 2018 г., 20:29:16 UTC+5 пользователь Nicola Furnari написал:
Reply all
Reply to author
Forward
0 new messages