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);     });   } 
  1. is correct way of adding association between existing user , existing course? (if not, correct way)

  2. i have ability of removing association when clicking on button. can't quite figure out how set function.

  1. if have user , course instance , associating them in adduser, yes, correct way of creating association between them.
  2. 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

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -