Reputation: 2838
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
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