jeudi 26 mars 2015

Sequelize hasMany through another table

Okay so i have the following three models


Module:



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


Competence:



Competence = sequelize.define('competence', {
id: DataTypes.INTEGER,
name: DataTypes.STRING,
organization_id: DataTypes.INTEGER,
competence_type_id: DataTypes.INTEGER
},{freezeTableName:true})


Module_has_competence:



Module_has_competence = sequelize.define('module_has_competence', {
id: DataTypes.INTEGER,
module_id: DataTypes.INTEGER,
competence_id: DataTypes.INTEGER,
score: DataTypes.STRING
},{
freezeTableName: true}
})


As you can see the relation between the tables are an n:m


So now i want to find all the Competence that a Module has:


So i created the following relationship:



Module.hasMany(Competence, {through: Module_has_competence, foreignKey: 'module_id'});


However when i try to run:



retrieveById: function (quote_id, onSuccess, onError) {
Module.find({include: [{ all: true }],where: {id: quote_id}})
.success(onSuccess).error(onError);
}


it returns nothing. But if i delete the relationship it returns only the Module


Can anyone tell me what i am doing wrong?


When i debug


When i debug it does not log any sql sadly it seems it is just ignoring the sql call ?


Aucun commentaire:

Enregistrer un commentaire