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

203
Views
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 answers
Answer question

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 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!