0

I want to perform an INNER JOIN using sequelize and Node. The actual SQL query looks like this:

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

Which gives the following response: response

Using sequelize as my ORM, I have tried to accomplish the above like this:

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

But then the response is different, i.e. as below: response

My DB: db

I have tried to search for answers, but without any luck.

Joel
  • 184
  • 1
  • 10
  • Possible duplicate of [How to make join queries using Sequelize on Node.js](https://stackoverflow.com/questions/20460270/how-to-make-join-queries-using-sequelize-on-node-js) – Charlie Schliesser Oct 30 '19 at 19:45

1 Answers1

0

You should give the name of the Model Framework you use.

Smartree
  • 54
  • 5