Friday 19 May 2017

Include joined table records by a specific query, if any

Let's assume that I want to select an Author, includeing their books published in 1942.

I tried doing this:

Author.findOne({
  where: {
    name: "Alice"
  },
  include: [{
    model: Book,
    where: {
      year: 1942
    }
  }]
}).then(authorAndTheirBooks => { ... })

authorAndTheirBooks is going to be null if there is no Book published in 1942.

If I remove that filter, it works fine.

How to solve this? To fetch the author, including their books, which were published in 1942?



via Ionică Bizău

No comments:

Post a Comment