1

How do you perform a simple inner join without eagerly loading the results of the JOIN table in Sequelize?

The following query correctly joins the books table and only returns records that have an associated books record, great.

I don't want the data from the books table to be populated in the dataset though.

Example:

If the parent model is say: a shelf. I want to find all the shelves that currently have any books in them...but I do not want to return all the book data, I just need the parent shelf records.

this.getModel().findAll(
{
    include: [
        {
            model: this.db.getModel('books'),
            required: true,
        }
    ]
});

How do I just return the parent table data based on the join? The docs are very confusing.

Justin
  • 4,203
  • 7
  • 41
  • 58
  • 1
    You are going to need to perform a custom query. JOINs are for actually JOINing data - not for loading data that meets a certain condition. You need to build a `WHERE id IN (SELECT shelf_id FROM books)`: – Ryan Wheale Jun 22 '17 at 21:16
  • Ok, this makes sense I think. Thanks. – Justin Jun 22 '17 at 21:32
  • 1
    Here's an example: https://stackoverflow.com/questions/36164694/sequelize-subquery-in-where-clause – Ryan Wheale Jun 22 '17 at 21:33

0 Answers0