I have a Ticket model and a Comment model. The Ticket has a hasMany relationship to Comment model. I want to search tickets by a keyword. The keyword will be matched againts
const { Op } = require("sequelize");
var options = {
where: {
[Op.or]: [
{ 'subject': { [Op.like]: '%' + query + '%' } },
{ '$Comment.body$': { [Op.like]: '%' + query + '%' } }
]
},
include: [{ model: Comment }]
};
Op.iLike
can be used for case-insensitive search.
Probably a bit too late for you, but for anyone else; #3095 was updated with a bit of a solution:
var options = {
where: {
$or: [
{ 'subject': { like: '%' + query + '%' } },
{ '$Comment.body$': { like: '%' + query + '%' } }
]
},
include: [{ model: Comment }]
};
The trick is in those dollar signs, though there are still problems with this solution when limit
is set or when querying with findAndCountAll()
.