简体   繁体   English

在源中创建行并与Sequelize多对多模型关联

[英]Creating row in both source and association with Sequelize many to many model

I have a many to many relationship between a user and a group. 我在用户和组之间有很多关系。 A user has many groups and a group has many users. 一个用户有多个组,一个组有许多用户。

I have a users table, a groups table and junction table called usersGroups. 我有一个用户表,一个组表和一个名为usersGroups的联结表。

My User model: 我的用户模型:

'use strict';
module.exports = function(sequelize, DataTypes) {
  var User = sequelize.define('User', {
    firstName: DataTypes.STRING,
    lastName: DataTypes.STRING,
    email: DataTypes.STRING,
    username: DataTypes.STRING,
    facebookId: DataTypes.STRING,
    password: DataTypes.STRING,
  }, {
    classMethods: {
      associate: function(models) {
        User.hasMany(models.Payment);
        User.hasMany(models.Friend, {foreignKey: 'userIdLink1', allowNull: false});
        User.belongsToMany(models.Group, { as: 'Groups', through: 'usersGroups', foreignKey: 'userId' });
      }
    },
    instanceMethods: {
      toJSON: function () {
        var values = Object.assign({}, this.get());

        delete values.password;
        return values;
      }
    }
  });
  return User;
};

My group model 我的团体模特

'use strict';
module.exports = function(sequelize, DataTypes) {
  var Group = sequelize.define('Group', {
  }, {
    classMethods: {
      associate: function(models) {
        // associations can be defined here
        Group.belongsToMany(models.User, { as: 'Users', through: 'usersGroups', foreignKey: 'groupId' });
      }
    },
    instanceMethods: {
      toJSON: function () {
        var values = Object.assign({}, this.get());

        delete values.password;
        return values;
      }
    }
  });
  return Group;
};

When I try create a new group with associated user with the following 2 methods, it creates a new group but no association 当我尝试使用以下2种方法与关联用户创建新群组时,它将创建一个新群组但没有关联

const values = {
  userId: 1
}

const options = {
  include: db.Users
}

db.Group
  .create(values, options)
  .then( (group) => {
    res.send(group)
  }).catch(err => {
    res.status(500).send({err: err})
  })

or 要么

db.Group
  .create()
  .then( (group) => {

    group.addUser({ userId: 1 }).then(result => {
        res.send(result)
    }).catch(err => {
        res.status(500).send({err: err})
    })

  }).catch(err => {
    res.status(500).send({err: err})
  })

If you simply want to assign user to newly created group, you need to use addUser , just like you did, but this method accepts first parameter as instance of Model or ID of instance, just like the documentation says 如果您只是想将用户分配给新创建的组,则需要像以前一样使用addUser ,但是此方法接受第一个参数作为Model的实例或实例的ID,就像documentation所说的那样

An instance or primary key of instance to associate with this. 实例或与此实例相关联的主键。

So you would have to perform group.addUser(userId).then(...) which in your case would be group.addUser(1).then(...) . 因此,您将必须执行group.addUser(userId).then(...) ,在您的情况下将是group.addUser(1).then(...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM