Populate joined table on create
See original GitHub issueI have 2 models in many to many relationship, so it has joined table. Users and Events, event_participants(joined table). So when im creating event i wanna populate this joined table with event id and ids of all users that i pass in request. For some reason it is trying to create users instead of just adding their ids. How to fix this? So on create need to populate joined table?
events.belongsTo(models.users, {
foreignKey: {
name: 'creatorId',
allowNull: false
},
onDelete: 'CASCADE',
as: 'creator'
});
events.belongsToMany(models.users, {
through: 'event_participants',
as: 'participants',
foreignKey: 'eventId',
otherKey: 'userId'
});
models.users.belongsToMany(events, {
through: 'event_participants'
});
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
SQL JOIN TABLES: Working with Queries in SQL Server
SQL JOIN tables queries can be divided into four main types: INNER JOIN; LEFT JOIN; RIGHT JOIN; FULL JOIN. Before we see the...
Read more >how to populate the join table with the create method in the ...
I'm trying to populate the join-table with the id's from two other tables, adding the user id is relatively easy because of devise...
Read more >SQL how to populate Join table - Programming - Linus Tech Tips
I am taking computer science and im working on database design. This is my second join table, but its essentially the same format...
Read more >Merge queries and join tables - Microsoft Support
Use Power Query to create a new product category by merging queries and creating join relationships.
Read more >Many-to-many relationships
To set up a join table for a many-to-many relationship: ... 1. Using the example above, create a table named Enrollments. This will...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In case anyone needs, months later i found solution for this
And i call this function in after hooks on create and patch like::
All of this happens in hooks of Event service. So when i create event, i get its participants and save it to joined table. Wish i knew this earlier. And stuff like this is NOT IN THE DOCS.
Thanks @kerimdragolj Your solution worked for me also. But for anyone else reading this issue in the future, don’t forget to pass
raw: false
tocontext.params.sequelize
. Because ifraw: true
, sequelize not adding the extra methods to associations.