alphadogg
alphadogg

Reputation: 12900

Sequelize belongsToMany

I have used Sequelize and setup a many-to-many relationship using belongsToMany(). However, when I query out data, like this:

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

I get an activity is not associated to permission! error. Why? Doesn't the fact that the belongsToMany connects Activity through Permission to Operator imply association?

My Permission model:

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'});

        }
      }
    }
  );

}

Upvotes: 3

Views: 3648

Answers (3)

thanh1101681
thanh1101681

Reputation: 302

Which I myself resolved as follows:

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

you only call:

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

if you want using:

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

You must declared

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

^^ good luck to u!!!! ^^.

Upvotes: 6

Jar An
Jar An

Reputation: 1

How about this? I remembered the relationship table data will be default wrapped in joined table data object.

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

Upvotes: 0

Jan Aagaard Meier
Jan Aagaard Meier

Reputation: 28778

Through models are not directly associated to any models. To be able to include the through model you need permissions.belongsTo(activity) or similar

Upvotes: 0

Related Questions