Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

        }
      }
    }
  );

}
like image 479
alphadogg Avatar asked May 27 '26 16:05

alphadogg


1 Answers

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!!!! ^^.

like image 127
thanh1101681 Avatar answered Jun 04 '26 17:06

thanh1101681



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!