Sequelize - join 4 tables

I am trying to link 4 tables. Tasks, tasks, questions and parameters.

enter image description here

My models are as follows

Task Model:

var Sequelize = require('sequelize'); module.exports = function(sequelize, DataTypes) { var Task = sequelize.define('Task', { task_id: { type: Sequelize.STRING, primaryKey: true }, task_name: { type: Sequelize.STRING, allowNull: true }, task_description: { type: Sequelize.STRING, allowNull: true } },{ classMethods: { associate: function(models) { Task.belongsToMany(models.Question, { through: { model: models.TaskQuestion }, foreignKey: 'task_id' }) } } }); return Task; }; 

TaskQuestions Model:

 var Sequelize = require('sequelize'); module.exports = function(sequelize, DataTypes) { var TaskQuestion = sequelize.define('TaskQuestion', { tq_id: { type: Sequelize.STRING, primaryKey: true } }); return TaskQuestion; }; 

Question Model:

 var Sequelize = require('sequelize'); module.exports = function(sequelize, DataTypes) { var Question = sequelize.define('Question', { question_id: { type: Sequelize.STRING, primaryKey: true }, question_description: { type: Sequelize.STRING, allowNull: true }, question_type: { type: Sequelize.STRING, allowNull: true } },{ classMethods: { associate: function(models) { Question.hasMany(models.Option, { foreignKey: { name: 'question_id', allowNull: false } }), Question.belongsToMany(models.Task, { through: { model: models.TaskQuestion }, foreignKey: 'question_id' }) } } }); return Question; }; 

Model Option:

 var Sequelize = require('sequelize'); module.exports = function(sequelize, DataTypes) { var Option = sequelize.define('Option', { option_id: { type: Sequelize.STRING, primaryKey: true }, question_id: { type: Sequelize.STRING, allowNull: true }, option_description: { type: Sequelize.STRING, allowNull: true }, option_type: { type: Sequelize.STRING, allowNull: true } },{ classMethods: { } }); return Option; }; 

When I try to extract data

 router.get('/:task_id', function(req, res) { models.Task.findOne({ where: { task_id: req.params.task_id }, include: [ models.Question ] }).then(function(task) { res.json(task); }); }); 

All I get is the connection between Task and Questions. When I get answers to questions separately, I get options below it. But he cannot get everything at once.

Is it possible. And let me know if you are following the right approach to creating a database in this format.

I need one task to contain several questions, and some questions may appear in several tasks. Each question should contain several options.

0
source share
1 answer

Yes, this is possible and is described in the documentation http://docs.sequelizejs.com/en/latest/docs/models-usage/#nested-eager-loading

Basically, instead of placing a model of type models.Question in your include array, you put an object with a key for model and a key for nested include

For your example above, something like this should do the trick:

 router.get('/:task_id', function(req, res) { models.Task.findOne({ where: { task_id: req.params.task_id }, include: [{ model: models.Question, include: [models.Option] }] }).then(function(task) { res.json(task); }); }); 
+1
source

All Articles