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.

"order by" appeared twice in generated sql which cause side effect - " Unknown column 'question.created_at' in 'order clause'"

See original GitHub issue

What 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:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
PurnimaLambaSGcommented, Jul 8, 2021

I am facing this same issue

1reaction
sushantdhimancommented, Feb 5, 2018

Can you try with latest version?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unknown column 'question.created_at' in 'order clause'
"order by" appeared twice in generated sql which cause side effect - " Unknown column 'question.created_at' in 'order clause'"
Read more >
Unknown column in order clause - mysql - Stack Overflow
Open your phpmyadmin. Click on your selected database. Now you have a list of all tables on right side.
Read more >
2 Server Error Message Reference - MySQL :: Developer Zone
Occurs for failure to create or copy a file needed for some operation. Possible causes: Permissions problem for source file; destination file already...
Read more >
QUERYING THE DATABASE: QUERIES and VIEWS
ORDER BY column list. Select from two tables: Example. Run the Orders Query (Orders Qry on the Query list): It lists all orders...
Read more >
sequelize unknown column 'createdat' in 'field list' - You.com
I think the error is that you have timestamps enabled in sequelize, but your actual table definitions in the DB do not contain...
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