question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Populate joined table on create

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

6reactions
kerimdragoljcommented, Feb 25, 2019

In case anyone needs, months later i found solution for this

function includeParticipants(hook) {
    if (hook.data.participants) {
        let participants = [hook.data.participants]; //convert data form request to array.
        participants.forEach((el) => {
            hook.result[0].setWorkers(el);
        });
    }
    return hook;
}

And i call this function in after hooks on create and patch like::


after: {
        all: [],
        find: [],
        get: [],
        create: [
            hook => includeWorkers(hook),
        ],
        update: [],
        patch: [
            hook => includeWorkers(hook),
        ],
        remove: []
    },

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.

2reactions
foadyouseficommented, Sep 8, 2018

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 to context.params.sequelize. Because if raw: true, sequelize not adding the extra methods to associations.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found