Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

481
Views
Node JS Sequelize WHERE LIKE operator in raw query is throwing error

I am building a Node JS application. I am using Sequelize for the database operations. Now, I am having a problem with using the LIKE operator with raw queries.

I have the query as follow.

let offset = (page > 1)? (page - 1) * recordPerPage: 0;
    let where = { }
    let sql = 'SELECT "Users"."id", "Users"."name", "Users"."email"';
    sql = sql + ' FROM "Users"';
    sql = sql + ' WHERE "Users"."id" != :myUserId';
    where.myUserId = myUserId;
    if (keyword) {
      where.keyword = keyword;
      sql = sql + ' AND ("Users"."email" LIKE "%:keyword%" OR "Users"."name" LIKE "%:keyword%")'
    }
    if (parseInt(roleType) > 0) {
      // get the role is because role is the type
      let role = await Role.findOne({
        where: {
          type: {
            [Op.eq]: roleType
          }
        }
      })
      where.role = role.id;
      sql = sql + ' AND "Users"."id" IN (SELECT "UserRoles"."user_id" FROM "UserRoles" WHERE "UserRoles"."role_id" =:role)';
    }
    sql = sql + ' ORDER BY "Users"."name" ASC, "Users"."id" ASC';

    where.offset = offset;
    where.limit = recordPerPage;
    sql = sql + ' LIMIT :limit OFFSET :offset';

    let users = await database.sequelize.query(sql, {
      replacements: where,
      type: QueryTypes.SELECT
    })

When I pass the keyword that is used with the LIKE operator, I am getting the following error.

`column "%'TEST'%" does not exist`

What is wrong with my code and how can I fix it?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Please take a look at the last example in here:
there is no " for the placeholder in the sql, but also, the keyword should include the % inside, as part of its content.

Here's the example from the docs:

const { QueryTypes } = require('sequelize');

await sequelize.query(
  'SELECT * FROM users WHERE name LIKE :search_name',
  {
    replacements: { search_name: 'ben%' },
    type: QueryTypes.SELECT
  }
);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!