Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

There was a problem

You must be a member of this account.

This project is archived and is in readonly mode.

PostgreSQLAdapter: Postgres Error while finding when using a function with two parameters in order clause

#1207

I am using Postgis, which provides functions for calculating distances in the database. One is ST_Distance(point1, point2). When using associations, order by AND limit (or will_paginate's paginate method) an error is thrown because the postgresql adapter splits the order clause at each ",".

For :order => "ST_distance(placemarks.geo, other.geo)" this leads to the following query:

: SELECT * FROM (SELECT DISTINCT ON ("placemarks".id) "placemarks".id, ST_distance(placemarks.geo AS alias_0, other.geo) AS alias_1 FROM "placemarks" ...

throwing an ActiveRecord::StatementInvalid error.

The logic to parse the order clause is wrong. It is probably very hard to use regexs to parse the list and not split functions, so I thought using an array for multiple order by clauses would be a good idea.

I slightly modified the Postgres Adapter with a patch which seems to solve this problem for me.

What do you think, should this be a general convention for rails to allow :order => ["column1", "column2"] instead of :order => "column1, column2" as this solves parsing problems?

Reported by Sascha Konietzke · October 13th, 2008 @ 11:18 AM

State: stale
Milestone: 3.x
Assigned to: Tarmo Tänav Tarmo Tänav
Importance: none

Activity

  1. Pratik
    Pratik
    • Assigned user set to Tarmo Tänav

    January 18th, 2009 @ 06:56 AM

  2. jay
    jay

    The old Trac-based dev site has a similar issue posted along with a patch that may help:

    http://dev.rubyonrails.org/ticke...

    I ran across a related issue yesterday when using PostgreSQL 8.3's NULLS FIRST/LAST clause in ordering. The aforementioned patch may help as it contains a small parser that looks for parenthesis and the like in function calls and also handles the NULLS FIRST/LAST issue, so I'll give it a shot and see how it goes.

    March 17th, 2009 @ 03:48 PM

  3. jay
    jay

    Okay, I've applied the aforementioned patch courtesy of gravatite against the master head and attached the diff. I had a go at a couple of tests, but frankly, not being overly familiar with ActiveRecord's tests, I don't know how complete or useful they are. They're there, though, and at least the patch doesn't appear to break any existing tests.

    March 17th, 2009 @ 07:08 PM

  4. Ruy Asan
    Ruy Asan

    I also have ran into this problem when trying to use postgresql's tsearch module for fulltext search.

    In general, any use of functions as order-by clauses will trigger this. Letting :order take arrays is going to be by far the best solution i think.

    June 20th, 2009 @ 11:57 PM

  5. Jeremy Kemper
    Jeremy Kemper
    • Milestone changed from 2.x to 3.x

    May 4th, 2010 @ 06:48 PM

  6. Santiago Pastorino
    Santiago Pastorino
    • State changed from new to open
    • Importance changed from to

    This issue has been automatically marked as stale because it has not been commented on for at least three months.

    The resources of the Rails core team are limited, and so we are asking for your help. If you can still reproduce this error on the 3-0-stable branch or on master, please reply with all of the information you have about it and add "[state:open]" to your comment. This will reopen the ticket for review. Likewise, if you feel that this is a very important feature for Rails to include, please reply with your explanation so we can consider it.

    Thank you for all your contributions, and we hope you will understand this step to focus our efforts where they are most helpful.

    February 2nd, 2011 @ 05:04 PM

  7. Santiago Pastorino
    Santiago Pastorino
    • State changed from open to stale

    February 2nd, 2011 @ 05:04 PM