Reputation: 20555
I have the following setup:
var Module = sequelize.define('module', {
id: DataTypes.INTEGER,
name: DataTypes.STRING,
description: DataTypes.STRING,
category_id: DataTypes.STRING,
module_type_id: DataTypes.STRING,
gives_score: DataTypes.INTEGER,
duration: DataTypes.STRING,
price: DataTypes.STRING
}, {
freezeTableName: true,})
Organization_has_module
Organization_has_module = sequelize.define('organization_has_module', {
id: DataTypes.INTEGER,
module_id: DataTypes.INTEGER,
organization_id: DataTypes.INTEGER
},{freezeTableName:true});
Module.hasMany(Organization_has_module, {foreignKey: 'module_id'});
Now im trying to find all modules where Organization_has_module.organization_id = 1
However im not quite sure how to do it
I have the following code:
Module.findAll({include: [{ all: true }],where: {module_type_id: type_id}})
.success(onSuccess).error(onError);
Upvotes: 1
Views: 320
Reputation: 2180
I think this is the same issue as your other question?
.hasMany()
doesn't reference a N:M
relationship, instead it its a 1:M
. You need .belongsToMany()
to reference a N:M
due to your join table.
Organization.belongsToMany(Module, {
through: Organization_has_module,
as: 'module',
foreignKey: 'module_id'
})
Module.belongsToMany(Organization, {
through: Organization_has_module,
as: 'organization',
foreignKey: 'organization_id'
})
Upvotes: 2