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

204
Vistas
Sequelize: column name to lowercase not working with included models

I have two models: Category and Bookmark. Bookmark is associated with category through categoryId column.

Category also have name, createdAt, orderId columns;


I can get category and all its bookmarks with:

const category = await Category.findOne({
    where: { id: req.params.id },
    include: [
      {
        model: Bookmark,
        as: 'bookmarks'
      },
    ]
  });

But now I want to change how bookmarks are ordered. Order type can be name, createdAt or orderId;

const order = orderType == 'name'
  ? [[
      { model: Bookmark, as: 'bookmarks' },
      Sequelize.fn('lower', Sequelize.col('bookmarks.name')),
      'ASC'
    ]]
  : [[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC']]

const category = await Category.findOne({
    where: { id: req.params.id },
    include: [
      {
        model: Bookmark,
        as: 'bookmarks'
      },
    ],
    order
  });

It works fine when orderType is createdAt or orderId but fails when it's name


I'm getting this error: SQLITE_ERROR: near \"(\": syntax error and SQL query generated by Sequelize is:

SELECT `Category`.`id`, `Category`.`name`, `bookmarks`.`id` AS `bookmarks.id`, `bookmarks`.`name` AS `bookmarks.name`, `bookmarks`.`categoryId` AS `bookmarks.categoryId`, `bookmarks`.`orderId` AS `bookmarks.orderId`, `bookmarks`.`createdAt` AS `bookmarks.createdAt`
FROM `categories` AS `Category`
INNER JOIN `bookmarks` AS `bookmarks` ON `Category`.`id` = `bookmarks`.`categoryId`
ORDER BY `bookmarks`.lower(`bookmarks`.`name`) ASC;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Changed it to:

const order =
    orderType == 'name'
      ? [[Sequelize.fn('lower', Sequelize.col('bookmarks.name')), 'ASC']]
      : [[{ model: Bookmark, as: 'bookmarks' }, orderType, 'ASC']];

and now it's working.

about 4 years ago · Juan Pablo Isaza 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