Sequelize error: [...]. belongsToMany([...]) requires through option, pass either a string or a model

Asked

Viewed 204 times

0

I’m importing models using the autoloader (index.js) of the sequelize. An Accesslevel model has a relationship with the Menu model (N: N), through the Accesslevelhasmenu model.

What should happen?
When importing this autloader it should call the models and inject the sequelize connection into them and return them.

What’s going on
When importing the autoloader (models/index.js) the following error message is displayed:
SequelizeAssociationError: AccessLevel.belongsToMany(Menu) requires through option, pass either a string or a model

NOTE: I am using this stack with Electron-webpack, nodejs v12 and sequelize " 5.21.7"

Accesslevel Model

    'use strict';
module.exports = (sequelize, DataTypes) => {
  const AccessLevel = sequelize.define('AccessLevel', {
    idAccessLevel: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true,
      allowNull: false
    },
    label: {
      type: DataTypes.STRING,
      allowNull: false
    },
  }, {});
  AccessLevel.associate = function (models) {
    // associations can be defined here
    console.log("É AQUI ---------")
    console.log(models)
    AccessLevel.belongsToMany(models.Menu, {
      trough: "AccessLeveHasMenu",
      foreignKey: "idAccessLevel"
    })
  };
  return AccessLevel;
};

Menu Model

'use strict';
module.exports = (sequelize, DataTypes) => {
const Menu = sequelize.define('Menu', {
  idMenu: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true,
    allowNull: false
  },
  label: {
    type: DataTypes.STRING,
    allowNull: false
  }
}, {});
Menu.associate = function (models) {
  // associations can be defined here
  Menu.belongsToMany(models.AccessLevel, {
    trough: "AccessLevelHasMenu",
    foreignKey: "idMenu"
  })
};
return Menu;
}

Accesslevelhasmenu Model

'use strict';

module.exports = (sequelize, DataTypes) => {
  const AccessLevelHasMenu = sequelize.define('AccessLevelHasMenu', {
    id: {
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
      type: DataTypes.INTEGER
    },
    idAccessLevel: {
      type: DataTypes.INTEGER,
      allowNull: false,
    },
    idMenu: {
      type: DataTypes.INTEGER,
      allowNull: false
    }
  }, {});
  AccessLevelHasMenu.associate = function (models) {
    // associations can be defined here
    AccessLevelHasMenu.belongsTo(models.AccessLevel, {
      foreignKey: "idAccessLevel"
    })
    AccessLevelHasMenu.belongsTo(models.Menu, {
      foreignKey: "idMenu"
    })
  };
  return AccessLevelHasMenu;
};

Autoloader (models/index.js)

'use strict';

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const { DataTypes } = require('sequelize')
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/database.json')[env];
const db = {};

let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
  sequelize = new Sequelize(config.database, config.username, config.password, config);
}

fs
  .readdirSync(__dirname)
  .filter(file => {
    return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
  })
  .forEach(file => {
    console.log(file)
    const model = require("./" + file)(sequelize, DataTypes);
    db[model.name] = model;
  });

Object.keys(db).forEach(modelName => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

1 answer

2


Hello, you put trough instead of through in your Accesslevel Model and Menu Model, probably now it’s right, I hope it helps.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.