Reputation: 1797
It is a conditional query. So that's why the code begins with:
var where = [];
Then I always put the ID for searching:
where.push({myId: myId});
I then check if conditions are met and in that case add a LIKE:
if(req.query.search !== undefined && req.query.search != ''){
where.push({name: {$like: req.query.search}});
}
When looking at the generated query I see:
SELECT "id", "name" FROM "MyTable" WHERE ("MyTable"."myId" = 1 AND "MyTable"."name" LIKE '''%''ABCD''%''');
As you can see a lot of quotes are added.
If I manually remove all the quotes but the first and the last, then the query executes well.
Why is sequelize adding all those quotes? Am I using the LIKE operator in the wrong way?
Upvotes: 0
Views: 755
Reputation: 13489
I faced this case recently and didn't found any issues related. Both like
and $like
didn't work in my case. And after some trials this works:
model.findAll({
where: Sequelize.where(Sequelize.col('columnName'), {
like: `%${string}%`,
}),
})
Upvotes: 1