javascript - how to remove relation for specific instances with sequelize / mysql -
i created many-to-many association between courses , users this:
course.belongstomany(user, { through: 'courseuser'}); user.belongstomany(course, { through: 'courseuser'}); which generates join table called courseuser.
i add relation specific course , user following server side function happens on click of button: usercourse variable looks this: { userid: 7, courseid: 13 }
adduser: function(req, res) { var usercourse = req.body; db.courseuser.create(usercourse).then(function () { res.sendstatus(200); }); } is correct way of adding association between existing user , existing course? (if not, correct way)
i have ability of removing association when clicking on button. can't quite figure out how set function.
- if have
user,courseinstance , associating them inadduser, yes, correct way of creating association between them. - you should able remove association in @ least 4 ways:
1: through user model:
user.removecourse(courseobject); 2: through course model:
course.removeuser(userobject); 3: on courseuser model, without having courseuser instance available:
db.courseuser.destroy({ where: {...} }); 4: having courseuser instance available:
courseuser.destroy(); you can bind .then , .catch on these operations on success / on error.
Comments
Post a Comment