many-to-many - Sequelize belongsToMany

标签 many-to-many associations sequelize.js

我使用了 Sequelize 并使用 belongsToMany() 设置了多对多关系。但是,当我查询出数据时,像这样:

api.models.operator.find({ 
      where: { id: connection.params.id }, 
      include: [ 
        { model: api.models.permission, 
          include: [
            { model: api.models.activity }
          ]
        } 
      ]
    })

我收到一个 activity is not associated to permission! 错误。为什么? belongsToMany 通过 Permission 将 Activity 连接到 Operator 是否意味着关联?

我的权限模型:

module.exports = function(sequelize, DataTypes) {

  return sequelize.define("permission", 
    {
      id: {
        type: DataTypes.BIGINT,
        allowNull: false,
        primaryKey: true,
        autoIncrement: true
      },
      operator_id: {
        type: DataTypes.BIGINT,
        allowNull: false,
        references: 'operator',
        referencesKey: 'id',
        comment: "The Operator in this Permission."
      },
      activity_id: {
        type: DataTypes.BIGINT,
        allowNull: false,
        references: 'activity',
        referencesKey: 'id',
        comment: "The Activity in this Permission."
      },
      createdAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: DataTypes.NOW,
        comment: "Date of record creation."
      },
      updatedAt: {
        type: DataTypes.DATE,
        allowNull: false,
        defaultValue: DataTypes.NOW,
        comment: "Date of last record update."
      },
      deletedAt: {
        type: DataTypes.DATE,
        comment: "Date record was marked as deleted."
      }
    }, 
    {
      comment: "A Permission indicates an allowed Activity by an Operator, ex: superadmin is allowed to 'POST /workorders'.",
      classMethods: {
        associate: function(models) {

          models.operator.belongsToMany(models.activity, {through: models.permission, foreignKey: 'operator_id', onDelete: 'CASCADE', onUpdate: 'CASCADE'});
          models.activity.belongsToMany(models.operator, {through: models.permission, foreignKey: 'activity_id', onDelete: 'CASCADE', onUpdate: 'CASCADE'});

        }
      }
    }
  );

}

最佳答案

我自己解决如下:

ModelA.belongsToMany(ModelB, {
        through: 'ModelC',
        foreignKey: 'ModelAID'
    });

你只调用:

ModelA.findAll({
include: [{
    model: ModelB
}]}).then(function(success) {}, function(error) {});

如果你想使用:

ModelB.findAll({
include: [{
    model: ModelA
}]}).then(function(success) {}, function(error) {});

你必须声明

ModelB.belongsToMany(ModelA, {
        through: 'ModelC',
        foreignKey: 'ModelBID'
    });

^^ 祝你好运!!!! ^^.

关于many-to-many - Sequelize belongsToMany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29547686/

相关文章:

java - Android:如何在 Room 中的多对多关系中插入数据(Java)

python - 如何在不使用 for 循环的情况下在多对多字段上应用过滤器

ruby-on-rails-4 - rails 最佳实践命名约定通过连接表 has_many

oop - UML:一对多关系表示?

sql - Sequelize : Create instance with existing association

java - Hibernate多对多映射+连接表更新不起作用

php - MYSQL 高级多对多查询

ruby-on-rails - 为什么 association.build 不会将 parent_id 分配给它的 child ?

javascript - 如何打破 promise 中的for循环?

database - 仅从 sequelize 原始查询中获取 dataValues 而不是 Model 实例