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

293
Views
Getting an error 'Unknown column' in 'order clause' using TypeORM

I'm trying to create a query with TypeORM and MySQL. I keep getting the following error:

[Nest] 44806 - 12/09/2021, 2:37:03 PM ERROR [ExceptionsHandler] ER_BAD_FIELD_ERROR: Unknown column 'sort_order' in 'order clause' QueryFailedError: ER_BAD_FIELD_ERROR: Unknown column 'sort_order' in 'order clause'

My query is:

const { limit, page: skip, userLat, userLng, searchQuery, weekday, startHour, endHour } = options;
let stores;

// get only stores that open in the start and end hours range
const openHoursQuery = `
          '${startHour}' BETWEEN \`from\` AND \`to\` AND
          '${endHour}' BETWEEN \`from\` AND \`to\`
          AND weekday = ${weekday}
      `;

// get the distance from user's location to each store
const getDistanceQuery = `
          SQRT(
            POW(69.1 * (lat - ${userLat}), 2) +
            POW(69.1 * (${userLng} - \`long\`) * COS(lat / 57.3), 2)
          ) AS distance
        `;

stores = this.storeRepository
  .createQueryBuilder('store')
  .leftJoinAndSelect('store.hours', 'store_hours')
  .addSelect(userLat && userLng ? getDistanceQuery : '')
  .where(searchQuery ? `name LIKE '%${searchQuery}%'` : '')
  .andWhere(weekday && startHour && endHour ? openHoursQuery : '')
  .orderBy(userLat && userLng ? 'distance' : 'sort_order') //sort_order
  .take(limit)
  .skip(skip)
  .getManyAndCount();

return stores;

The problem is caused by the "leftJoinAndSelect" method, when I comment the join the query executes without any problems.

My DB tables look like this:

TABLE: stores

COLUMNS: id, uuid, name, status, address, URL, email, lat, long, sort_order

Table: store_hours

COLUMNS: id, store_id, weekday, from, to, type

EDIT:

I managed to understand the issue, I had to use store.sortOrder which is the name corresponding field in the 'store' Entity.

I have now a follow-up issue that sort by distance is not working when I use the 'join' method.

'distance' is an additional field I created in the select to sort the stores by the distance from the user.

Thank you

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Found the answer.

I should of use the Alise argument and not create alise myself.

Solution:

  .addSelect(userLat && userLng ? getDistanceQuery : '', 'distance')

  .orderBy(userLat && userLng ? 'distance' : 'store.sortOrder')
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!