最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - how to remove relation for specific instances with sequelizemysql - Stack Overflow

programmeradmin8浏览0评论

I created a many-to-many association between courses and users like this:

Course.belongsToMany(User, { through: 'CourseUser'});
User.belongsToMany(Course, { through: 'CourseUser'});

which generates a join table called CourseUser.

I add a relation for a specific course and user with the following server side function that happens on the click of a button: the userCourse variable looks like this: { UserId: 7, CourseId: 13 }

  addUser: function(req, res) {
    var userCourse = req.body;
    db.CourseUser.create(userCourse).then(function () {
      res.sendStatus(200);
    });
  }
  1. Is this the correct way of adding the association between an existing user and an existing course? (if not, what is the correct way)

  2. I would like to have an ability of removing the association when clicking on another button. But I can't quite figure out how to set up the function.

I created a many-to-many association between courses and users like this:

Course.belongsToMany(User, { through: 'CourseUser'});
User.belongsToMany(Course, { through: 'CourseUser'});

which generates a join table called CourseUser.

I add a relation for a specific course and user with the following server side function that happens on the click of a button: the userCourse variable looks like this: { UserId: 7, CourseId: 13 }

  addUser: function(req, res) {
    var userCourse = req.body;
    db.CourseUser.create(userCourse).then(function () {
      res.sendStatus(200);
    });
  }
  1. Is this the correct way of adding the association between an existing user and an existing course? (if not, what is the correct way)

  2. I would like to have an ability of removing the association when clicking on another button. But I can't quite figure out how to set up the function.

Share Improve this question edited Jan 17, 2016 at 0:40 jmancherje asked Jan 16, 2016 at 20:13 jmancherjejmancherje 6,6338 gold badges38 silver badges59 bronze badges 1
  • 1 Please accept my answer if it answered your question or tell me in a ment what part is still unclear to you. – leroydev Commented Feb 13, 2016 at 23:53
Add a ment  | 

2 Answers 2

Reset to default 12
  1. If you already have a User and Course instance and are just associating them in addUser, then yes, this is the correct way of creating an association between them.
  2. You should be able to remove this association in at least 4 ways:

1: Through User model:

User.removeCourse(courseObject);

2: Through Course model:

Course.removeUser(userObject);

3: On the CourseUser model, without having a CourseUser instance available:

db.CourseUser.destroy({
    where: {...}
});

4: Having a CourseUser instance available:

courseUser.destroy();

You can bind .then and .catch on all these operations to do something on success / on error.

Your model relationship is okay:

Course.belongsToMany(User, { through: 'CourseUser'}); User.belongsToMany(Course, { through: 'CourseUser'});

and values:
{ UserId: 7, CourseId: 13 }

Implementation could look thus for adding a UserCourse relationship:

addUser: function(req, res) {
    const { UserId, CourseId } = req.body;
    db.Course.findOne({
        where: { id: CourseId }
    }).then(course => {
        course.setUsers([UserId])
        res.sendStatus(200);
    }).catch(e => console.log(e));
}

while this could handle user detaching:
removeUser: function (req, res) {
    const { UserId, CourseId } = req.body;
    db.Course.findOne({
        where: { id: CourseId }
    }).then(course => {
        course.removeUsers([UserId])
        res.sendStatus(200);
    }).catch(e => console.log(e));
}

I hope this will be helpful however
发布评论

评论列表(0)

  1. 暂无评论