PostgreSQL Error "SELECT DISTINCT, ORDER BY expressions must appear in select list"

786 views
Skip to first unread message

fuga

unread,
Apr 3, 2011, 9:19:03 AM4/3/11
to web2py-users
web2py generate SQL that PostgreSQL doesn't accept.

For instance,

SELECT DISTINCT a.id, a.uid, a.name FROM a, b WHERE ((((a.deleted <>
'T') AND (a.id > 0)) AND (((b.deleted <> 'T') AND (b.id > 0)) AND
(b.id = 1))) AND (b.uid = a.uid)) ORDER BY b.id, a.id LIMIT 1 OFFSET
0;

It fails because select list is not contain "b.id".

I change gluon/dal.py(Version 1.94.5) as follows.
It is work well.
But I am not sure that it is a good practice.

=== modified file 'gluon/dal.py' Version 1.94.5
@@ -1039,2 +1039,6 @@
--------------------------------------------------------------
if not orderby and tablenames:
- sql_o += ['%s.%s'%(t,x) for t in tablenames for x in
((hasattr(self.db[t],'_primarykey') and self.db[t]._primarykey) or
[self.db[t]._id.name])]
+ orderby_fields = ['%s.%s'%(t,x) for t in tablenames for x in
((hasattr(self.db[t],'_primarykey') and self.db[t]._primarykey) or
[self.db[t]._id.name])]
+ sql_o += ' ORDER BY %s' % ', '.join(orderby_fields)
+ for orderby_field in orderby_fields:
+ if orderby_field not in self._colnames:
+ sql_f += ', %s' % orderby_field


Your opinion will be much appreciated,
Cheers,

Massimo Di Pierro

unread,
Apr 3, 2011, 4:01:24 PM4/3/11
to web2py-users
Can you show us the web2py dal expression that caused the invalid
query? Thanks!

Ross Peoples

unread,
Apr 4, 2011, 8:12:38 AM4/4/11
to web...@googlegroups.com
Showing us the model would help too, especially if you are connecting to a legacy database.

fuga

unread,
Apr 4, 2011, 10:24:58 AM4/4/11
to web...@googlegroups.com
I attach argument-variables that is passed to BaseAdapter.select() (by
using pydev).

Do you mean as such?


I'm working for sahana-eden project.
It will be used for the earthquake rescue of Japan.

http://eden.sahanafoundation.org/wiki


PostgreSQL errors are caused by some querys which succeeds on other DBs.
I want you to cooperate in the solution.

Thanks

2011/4/4 Massimo Di Pierro <massimo....@gmail.com>:

variables

Massimo Di Pierro

unread,
Apr 4, 2011, 2:02:11 PM4/4/11
to web2py-users
We need to see the web2py expression that generates the problematic
query. From your comment it seems to me the problem is there.

On Apr 4, 9:24 am, fuga <fug...@gmail.com> wrote:
> I attach argument-variables that is passed to BaseAdapter.select() (by
> using pydev).
>
> Do you mean as such?
>
> I'm working for sahana-eden project.
> It will be used for the earthquake rescue of Japan.
>
> http://eden.sahanafoundation.org/wiki
>
> PostgreSQL errors are caused by some querys which succeeds on other DBs.
> I want you to cooperate in the solution.
>
> Thanks
>
> 2011/4/4 Massimo Di Pierro <massimo.dipie...@gmail.com>:
>  variables
> 2KViewDownload
Reply all
Reply to author
Forward
0 new messages