18

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?

1
  • I found the wording of your question a little confusing, so I edited it. I hope I understood correctly, please feel free to change it back if you think I screwed up. Commented Feb 17, 2012 at 0:32

6 Answers 6

26

Now on Sequelize you can try this

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

Good simplistic answer. Quick note for new users, title is the name of the column you are wanting to find the match against.
Thanks @JMM I fixed it ;)
18

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

Comments

6

Please try this code

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

Comments

4

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

Comments

0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.