Sequelize adding ORDER BY to subquery and main query causing ER_BAD_FIELD_ERROR
See original GitHub issueWhat 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:
- Created 7 years ago
- Reactions:1
- Comments:25
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
http://docs.sequelizejs.com/manual/querying.html#ordering
Same problem here! Someone has a solution?