Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

483
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda