Search code examples
rubysequel

Is it possible to order by a non-selected column in Sequel?


Given a table t with columns c1, c2 and cx, I have a failing Sequel (version 3.27.0) query of the form:

DB[:t].order(:cx).select{[:c1, :c2]}

I don't need the values from cx in my output, I just want them to be used for ordering.

The equivalent SQL would be:

SELECT c1, c2 FROM t ORDER BY cx;

My current workaround is to just ask for cx too, even though I don't need it:

DB[:t].order(:cx).select{[:c1, :c2, :cx]}

Can anyone confirm that this is a known behavior/bug in Sequel?


Solution

  • The SQL produced by Sequel for that code is: SELECT c1, c2 FROM t ORDER BY cx. If it's failing, it's most likely a problem with your database (you'd need to post the backtrace you are getting if you want to debug that).

    FWIW, I'd use the following Sequel code: DB[:t].order(:cx).select(:c1, :c2). There's no reason to use a virtual row block if you aren't using the features it provides.