mysql - Sequelize - 关联 4 个表

标签 mysql node.js sequelize.js

我正在尝试关联 4 个表。任务、任务问题、问题和选​​项。

enter image description here

我的模型如下

任务模型:

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

任务问题模型:

var Sequelize = require('sequelize');

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

问题模型:

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

选项模型:

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

当我尝试检索数据时

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

我得到的只是任务和问题之间的关联。当我单独检索问题时,我会在其下找到选项。但似乎无法一次检索全部。

有可能吗?让我知道我是否遵循正确的方法以这种格式设计数据库。

我需要一个任务包含多个问题,并且相同的问题可以出现在多个任务中。每个问题应包含多个选项。

最佳答案

是的,这是可能的,并且在 http://docs.sequelizejs.com/en/latest/docs/models-usage/#nested-eager-loading 的文档中进行了介绍。

基本上,不要放置像 models.Question 这样的模型在包含数组中,放置一个对象,其键为 model和嵌套 include 的键的

对于上面的示例,类似这样的操作应该可以解决问题:

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

关于mysql - Sequelize - 关联 4 个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41671377/

相关文章:

node.js - 如何在内容类型为='application/x-protobuf'的node.js中发布数据

javascript - 客户端 websocket 限制消息长度

javascript - 如何在查询中从表中获取属性?

javascript - Sequelize 树中的连接数据

mysql - 删除所有没有表的模式

php - 重置Wordpress 'next User ID'号码

node.js - ElasticSearch是否可以仅显示聚合?

带有 Sequelize 的 MySQL 全文搜索

mysql - MySQL 记录之间的笛卡尔积

mysql - WordPress 函数检查表是否为空