"order by" appeared twice in generated sql which cause side effect - " Unknown column 'question.created_at' in 'order clause'"
See original GitHub issueWhat are you doing?
I’m trying to do a query:
models.User.findAll({
limit:limit,
order:[['createdAt','DESC']],
include : [
{
model:models.Question,
as:"questions"
},]
})
// models are as below
module.exports = db.sequelize.define('user', {
// id is added automatically
createdAt: {
type: Sequelize.DATE,
field: 'created_at'
}
})
module.exports = db.sequelize.define('question', {
// id is added automatically
userId: {
type: Sequelize.BIGINT,
field: 'user_id'
}
})
User.hasMany(Question,{foreignKey:'userId',as:'questions'})
What do you expect to happen?
I expected that this query could run smoothly but…
What is actually happening?
I came across an exception : “SequelizeDatabaseError: Unknown column ‘user.created_at’ in ‘order clause’”
Output, either JSON or SQL: Generated sql is: SELECT user.* FROM (SELECT user.id, user.created_at AS createdAt FROM user AS user ORDER BY user.created_at DESC LIMIT 10) AS user LEFT OUTER JOIN question AS questions ON user.id = questions.user_id ORDER BY user.created_at DESC
seems the ‘Order by’ was applied twice. At the second time, the ‘created_at’ was renamed as ‘createdAt’ that’s why it complains “cannot find ‘created_at’”. Obviously the second ‘order by user.created_at’ is not necessary. If we could remove this clause, the problem will be resolved.
Dialect: mysql / postgres / sqlite / mssql / any
Dialect version: mysql
Database version:
Sequelize version: ^4.4.2
Tested with latest release: No (If yes, specify that version)
Note : Your issue may be ignored OR closed by maintainers if it’s not tested against latest version OR does not follow issue template.
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (2 by maintainers)
I am facing this same issue
Can you try with latest version?