PostgreSQL: invalid SQL when ordering by multiple columns in the same string #1720

Closed
bradediger opened this Issue Jun 15, 2011 · 0 comments

Comments

Projects
None yet
2 participants
Contributor

bradediger commented Jun 15, 2011

Is the AR::Relation syntax order("a asc, b asc") intended to be a valid way to sort on multiple columns? That syntax almost works but fails when the PostgreSQL adapter tries to grab the column names to include them in the SELECT list of a DISTINCT query (postgresql_adapter.rb:933-943 def distinct).

Here's a failing test against master (078bdc6) under PostgreSQL:

# test/cases/relations_test.rb
def test_finding_with_cross_table_order_and_limit
  tags = Tag.includes(:taggings).order("tags.name asc, taggings.taggable_type asc").limit(1).to_a
  assert_equal 1, tags.length
end

The test passes if you change the order statement to order("tags.name asc", "taggings.taggable_type asc") or order("tags.name asc").order("taggings.taggable_type asc"). I don't know whether the original syntax is supposed to be supported, but it would be nice to either make it work cleanly or raise an error. Thanks!

raviolicode added a commit to raviolicode/rails that referenced this issue Jun 24, 2011

Improve ordering of multiple columns on postgresql
* Only on postgresql, order("first asc, second asc") was invalid
* Closes #1720

raviolicode added a commit to raviolicode/rails that referenced this issue Jun 24, 2011

Improve ordering of multiple columns on postgresql
* Only on postgresql, order("first asc, second asc") was invalid
* Closes #1720
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment