Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

482
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda