Question

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?

Was it helpful?

Solution

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);
    }
});

OTHER TIPS

Now on Sequelize you can try this

{ where: { columnName: { $like: '%awe%' } } }

See http://docs.sequelizejs.com/en/latest/docs/querying/#operators for updated syntax

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

Please try this code

const Sequelize = require('sequelize');
const Op = Sequelize.Op;
{ where: { columnName: { [Op.like]: '%awe%' } } }

It might be cleaner to leverage the Sequelize.Utils.format function

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%"');

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top