henk
henk

Reputation: 2838

How to use an operator in where clause with an include?

I have the following Model:

AuthorModel.hasMany(BookModel);
BookModel.belongsTo(AuthorModel);

I want to select an author whose name or the title of one of his books matches the search string.

Something like:

            Author.findOne({
                where: {
                    [Op.or]: [
                        { name: 'test'},
                        {'books.title':'test'}
                    ]
                },
                include: [{
                    model: Book
                }]
            })

Whatever I try, I always end up with: Unknown column 'books.title' in 'where clause'

Upvotes: 1

Views: 545

Answers (1)

doublesharp
doublesharp

Reputation: 27599

You just need to move the where clause inside your include.

Author.findOne({
  include: [{
    model: Book,
    where: {
      [Op.or]: [
        { '$author.name$': 'test'},
        { title: 'test'}
      ]
    },
  }]
})

Upvotes: 1

Related Questions