grails / gorm-hibernate5

GORM for Hibernate 5

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

V7.0.5 with Postgres: list(max:...) causes exception on totalCount

Grestorn opened this issue · comments

I've just updated my rather large project to Grails 4.0.11 from 3.3. One problem which I couldn't fix seems to be a regression with GORM 7.0.5 on Postgres:

This simple code:

   def activity(Integer max) {
        max = max ?: 50
        params.max = max
        if(!params.sort)
            params.sort = "created"
        if(!params.order)
            params.order = "desc"

        def activityList = Activity.list(params)

        render view:"activity", model:[activityList: activityList, count: activityList.getTotalCount()]
    }

is translated by GORM into

select distinct count(activity0_.id) as col_0_0_ from grails_activity activity0_ order by activity0_.created desc

which, on postgresql causes the following exception

ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in select list

For "totalCount" there seems to be no necessity to even include the order clause to the SQL statement.

It can be worked around by changing the above code to:

def activity(Integer max) {
        max = max ?: 50
        params.max = max
        if(!params.sort)
            params.sort = "created"
        if(!params.order)
            params.order = "desc"

        def activityList = Activity.findAll(params) {
            created != null
        }

        render view:"activity", model:[activityList: activityList, count: activityList.getTotalCount()]
    }

still the bug deserves to be fixed.

There is a similar problem with MySQL when using sql_mode with ONLY_FULL_GROUP_BY.

It results in the following error:
ERROR 1140 (42000): Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause.