Reputation: 20448
I'm trying to follow the Sequelize tutorial on their website.
I have reached the following line of code.
Project.findAll({where: ["id > ?", 25]}).success(function(projects) {
// projects will be an array of Projects having a greater id than 25
})
If I tweak it slightly as follows
Project.findAll({where: ["title like '%awe%'"]}).success(function(projects) {
for (var i=0; i<projects.length; i++) {
console.log(projects[i].title + " " + projects[i].description);
}
});
everything works fine. However when I try to make the search parameter dynamic as follows
Project.findAll({where: ["title like '%?%'", 'awe']}).success(function(projects) {
for (var i=0; i<projects.length; i++) {
console.log(projects[i].title + " " + projects[i].description);
}
});
It no longer returns any results. How can I fix this?
Upvotes: 18
Views: 25565
Reputation: 141927
I think you would do that like this:
where: ["title like ?", '%' + 'awe' + '%']
So if you were doing this with an actual variable you'd use:
Project.findAll({where: ["title like ?", '%' + x + '%']}).success(function(projects) {
for (var i=0; i<projects.length; i++) {
console.log(projects[i].title + " " + projects[i].description);
}
});
Upvotes: 18
Reputation: 3460
Please try this code
const Sequelize = require('sequelize');
const Op = Sequelize.Op;
{ where: { columnName: { [Op.like]: '%awe%' } } }
Upvotes: 6
Reputation: 1
The accepted answer of ["columnName like ?", '%' + x + '%']
for the where clause results in this error in Sequelize 4.41.1: "Support for literal replacements in the where
object has been removed."
Assuming: modelName.findAll({ where : { columnName : { searchCriteria } } });
Using [Op.like]: '%awe%'
or $like: '%awe%' }
as the searchCriteria (where 'awe' is the value you want to find in columnName) both result in SQL with a LIKE clause of LIKE '\"%awe%\"'
. Notice the extra quotation marks. [Op.like] and $like are aliases of each other and neither answer the OP's question because they don't allow dynamic search parameters.
Using [Op.like] : `%${parameter}%`
as the searchCriteria (where 'parameter' is the parameter whose value you want to find in columnName) resulted in SQL with a LIKE clause of LIKE '\"%findMe\"'
when parameter= 'findMe'. Again, notice the extra quotation marks. No results.
An answer in another StackOverflow post suggested using [Op.like]: [`%${parameter}%`]
for the searchCriteria (where 'parameter' is the parameter whose value you want to find in columnName). Notice the square brackets! This resulted in SQL with a LIKE clause of LIKE '[\"%findMe%\"]'
when parameter= 'findMe'. Again notice the extra quotation marks and the square brackets. No results.
For me, the solution was to use a raw query:
Sequelize.query('SELECT * FROM tableName WHERE columnName LIKE "%searchCriteria%"');
Upvotes: 0
Reputation: 511
Now on Sequelize you can try this
{ where: { columnName: { $like: '%awe%' } } }
See http://docs.sequelizejs.com/en/latest/docs/querying/#operators for updated syntax
Upvotes: 26
Reputation: 9411
I would do it in this way:
Project.findAll({where: {title: {like: '%' + x + '%'}, id: {gt: 10}}).success(function(projects) {
for (var i=0; i<projects.length; i++) {
console.log(projects[i].title + " " + projects[i].description);
}
});
In this way you can have nicely more WHERE clausas
Upvotes: 4
Reputation: 1388
It might be cleaner to leverage the Sequelize.Utils.format function
Upvotes: 0