Same problem as described in http://drupal.org/node/742006 I guess. Fields used in a query ORDER BY clause aren't listed in the SELECT one, so PostgreSQL doesn't like it. This problem isn't viewable when using MySQL as DBMS.
PostgreSQL version: 8.3
OS: Debian squeeze/sid
PHP: 5.3.1-5
Apache: 2.2.14-7
Error description:
# warning: pg_query(): Query failed: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list in /var/www/drupal-6.16/includes/database.pgsql.inc on line 139.
# user warning: query: SELECT DISTINCT n.nid FROM drupal_node n INNER JOIN drupal_term_node tn ON n.nid = tn.nid WHERE tn.tid IN (7) AND n.type = 'image' AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC in /var/www/drupal-6.16/sites/all/modules/image/contrib/image_gallery/image_gallery.pages.inc on line 48.
Comments
Comment #1
fjleal commentedAdding ", n.sticky, n.created" to the select arguments list fixes the problem (also on line 56, so the query becomes "SELECT n.nid, n.sticky, n.created FROM {term_node}..." instead of "SELECT n.nid FROM {term_node}...". This works only for my particular ordering preferences, it's not a fix.
Comment #2
sebzur commentedThe same story with image_gallery_handler_field_gallery_cover.inc
Adding n.sticky, n.created to select list in 215 line of image_gallery_handler_field_gallery_cover.inc fulfils PostgreSQL query correctness. The full function responsible for query is:
Comment #3
joachim commentedhttp://www.postgresql.org/docs/8.2/static/sql-select.html#SQL-ORDERBY
> It is also possible to use arbitrary expressions in the ORDER BY clause, including columns that do not appear in the SELECT result list.
Are you guys on an older version of Postgres maybe?
Comment #4
sebzur commentedI'm using 8.3.1. The problem is possibly related to the DISTINCT Clause. I've just looked to the Postres DOC you've posted and I can see:
I'm not a SQL guru, but I guess this might be the cause of the problem.
Comment #5
joachim commented> # user warning: query: SELECT DISTINCT n.nid FROM drupal_node n INNER JOIN drupal_term_node tn ON n.nid = tn.nid WHERE tn.tid IN (7) AND n.type = 'image' AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC in /var/www/drupal-6.16/sites/all/modules/image/contrib/image_gallery/image_gallery.pages.inc on line 48.
Actually, I can't find this query. There is only one DISTINCT in that file, and it's:
Can you check you're on the latest version please?
Comment #6
sebzur commentedOn the last dev version problem still exist. I had to additionally add another element to select list: n.created
The whole function is now:
Comment #7
n3ko commentedHi! Not need SQL guru for this... just think about it:
SELECT DISTINCT n.nid FROM drupal_node n ORDER BY n.sticky DESC, n.created DESC
Now imagine this table:
nid|sticky
111 |0
112 |1
111| 2
And now ,,execute'' the DISTINCT:
nid|sticky
111| 0, 2
112| 1
See: we have different values in the one row. (of course database server cant do that simply drop the unneeded column, do the distinct, and then cant find the field, referenced in the order)
You can order it on sticky? You can't, because of the different values.
Its not a real possibility, because nid is the primary key of the node, and we order on other node fields, anyway we have modify the query.
We can add the fields to the distinct if the order is realy needed, or remove from order by if not.
Other option is use group by instead of distinct, and do some aggregate (min/max/avg) to the order clause:
SELECT n.nid FROM drupal_node n .. etc .. GROUP by 1 ORDER BY min(n.sticky) DESC, min(n.created) DESC
Comment #8
sunSorry, without further information this issue can only be closed as not reproducible.
Feel free to re-open this issue if you want to provide further information. Thanks.
Comment #9
nicetuna commentedHi,
In "modules/image/contrib/image_gallery/image_gallery.pages.inc", I have replaced (but didn't verify if it is still working on MySQL)
with
with
And
with
Comment #10
nicetuna commentedComment #11
nicetuna commentedComment #12
nicetuna commented