NODEJS-执行INNER JOIN续集

时间:2019-10-30 09:08:01

标签: mysql node.js sequelize.js

我想使用sequelize和Node执行INNER JOIN。实际的SQL查询如下所示:

SELECT b.id, b.title, b.author, b.image_url, s.novel_status AS status, g.genre 
FROM novels b 
  INNER JOIN genres g 
  ON b.genre = g.id 
  INNER JOIN novel_statuses s 
  ON b.status = s.id

哪个给出以下响应: response

使用sequelize作为我的ORM,我已经尝试完成上述操作:

getNovel: (req, res) => {
    novelModel.hasMany(statusModel, {
      foreignKey: 'id'
    });
    statusModel.belongsTo(novelModel, {
      foreignKey: 'id'
    });
    novelModel.hasMany(genreModel, {
      foreignKey: 'id'
    });
    genreModel.belongsTo(novelModel, {
      foreignKey: 'id'
    });

    novelModel
      .findAll({
        include: [
          {
            model: genreModel,
            required: true,
            attributes: ['genre']
          },
          {
            model: statusModel,
            required: true,
            attributes: ['novel_status']
          }
        ]
      })
      .then(result => res.json(result))
      .catch(err => res.json(err));
  },

但是随后的响应有所不同,即如下所示: response

我的数据库: db

我试图寻找答案,但是没有任何运气。

1 个答案:

答案 0 :(得分:0)

您应提供所使用的模型框架的名称。