question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sequelize adding ORDER BY to subquery and main query causing ER_BAD_FIELD_ERROR

See original GitHub issue

What you are doing?

When I add these two scopes, my publishedAt order gets added twice, once to the subquery (which is what i want), and once to the encasing query which causes a ER_BAD_FIELD_ERROR

FIELD_ERROR: Unknown column ‘Ticket.published_at’ in ‘order clause’

If I remove it from the outer query, the query works as expected, so my question is why would it be being added to both queries?

// Scope 1
published: function() {
 var data = {
   where: {
     status: 'published',
     publishedAt: {
       $lt: models.sequelize.fn('NOW')
     }
   },
   order: [
     ['publishedAt', 'DESC']
   ]
 };

 return data;
},


// Scope 2
decorated: function() {
  var includes = {
    include: [{
      model: models.Asset,
      as: 'FeaturedAsset'
    }, {
      model: models.Category
    }, {
      model: models.User,
      as: 'Author'
    }]
  };

  return includes;
}

What do you expect to happen?

I wanted it to order the inner query

What is actually happening?

It added the order to both inner and outer query.

SELECT ... FROM (
    SELECT ... FROM `tickets` AS `Ticket` WHERE (`Ticket`.`deleted_at` IS NULL AND (`Ticket`.`status` = 'published' AND `Ticket`.`published_at` < NOW() AND `Ticket`.`channel_id` = 3)) 
    ORDER BY `Ticket`.`published_at` DESC LIMIT 0, 6) AS `Ticket` ... 
ORDER BY `Ticket`.`published_at` DESC;

Dialect: mysql Database version: 5.6 Sequelize version: latest

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:1
  • Comments:25

github_iconTop GitHub Comments

8reactions
elias-summermattercommented, Jul 28, 2018

Same problem here! Someone has a solution?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sequelize Order within Query and Sub-Query - Stack Overflow
You can use the sorder subqueries the same way, add order attribute models.Blog.findAll({ order: 'blog_date DESC', limit: 10, include: [ { model: models....
Read more >
Sub Queries - Sequelize
Using sub-queries for complex ordering​. This idea can be used to enable complex ordering, such as ordering posts by the number of laugh...
Read more >
Hooks - Sequelize
A hook may contain async actions - in this case the hook function should return a promise. There are currently three ways to...
Read more >
Model Basics | Sequelize
Adding a Public Class Field will shadow those getter and setters, blocking access to the model's actual data. // Invalid class User extends ......
Read more >
Model Instances - Sequelize
This is because the build method only creates an object that represents data that can be mapped to a database. In order to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found