You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I tried to make two models of User(exist in this repo) and Team(new).
I added following in User model.
const Team = require("./Team");
Team.memberAssociation = User.belongsToMany(Team, { through: "member" });
How do I make this relationship in Team model, and how to add a user to Team in UserController.js in a method like register?
The text was updated successfully, but these errors were encountered:
// a team can have multiple membersTeam.belongsToMany(User,{as: 'teamMember',// this is how you can add, set or remove associationsthrough: 'team_members',// define the table, I would suggest to call it model1_model2 or if you have a different name for it then use the name as here model1_nameforeignKey: 'memberId',// I would always suggest to set your own if you want to work with the plain database});// a member can join multiple teamsUser.belongsToMany(Team,{as: 'membersTeam',through: 'team_members',foreignKey: 'teamId',});// useconstsomeUser=User.findByPk(1);Team.addTeamMember(someUser);Team.addTeamMember(2);Team.addTeamMember(3);// team has 1,2,3Team.removeTeamMember(3);// team has now member 1,2Team.setTeamMember([4,5,6])// team has now 4,5,6 and deletes other associations
Hi,
I tried to make two models of User(exist in this repo) and Team(new).
I added following in User model.
const Team = require("./Team");
Team.memberAssociation = User.belongsToMany(Team, { through: "member" });
How do I make this relationship in Team model, and how to add a user to Team in UserController.js in a method like register?
The text was updated successfully, but these errors were encountered: