Estou importando models usando o autoloader (index.js) do sequelize. Um model AccessLevel tem um relacionamento com o model Menu (N: N), através do model AccessLevelHasMenu.
O que deveria acontecer?
Ao importar esse autloader ele deveria chamar as models e injetar a conexão do sequelize nelas e retorná-las.
O que está acontecendo
Ao importar o autoloader (models/index.js
) a seguinte mensagem de erro é exibida:
SequelizeAssociationError: AccessLevel.belongsToMany(Menu) requires through option, pass either a string or a model
OBS: Estou utilizando essa stack com electron-webpack, nodejs v12 e 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;