Hi folks,

do you have evidence that the where query aliases are working for sorting?
It does not seem to work in my code.

The only difference I see is that I sort multiple columns with something
like:

    def query = buildQuery(filterParams)
    return query.list(sort: ['o1.firstName': 'desc'])



>>>
7.4.6. Query Aliases and Sorting

If you define a query for an association an alias is automatically
generated for the query. For example the following query:

def query = Pet.where {
    owner.firstName == "Fred"
}

Will generate an alias for the owner association such as owner_alias_0.
These generated aliases are fine for most cases, but are not useful if you
want to later sort or use a projection on the results. For example the
following query will fail:

// fails because a dynamic alias is used
Pet.where {
    owner.firstName == "Fred"
}.list(sort:"owner.lastName")

If you plan to sort the results then an explicit alias should be used and
these can be defined by simply declaring a variable in the where query:

def query = Pet.where {
    def o1 = owner
    o1.firstName == "Fred"
}.list(sort:'o1.lastName')

Define an alias called o1
Use the alias in the query itself
Use the alias to sort the results

By assigning the name of an association to a local variable it will
automatically become an alias usable within the query itself and also for
the purposes of sorting or projecting the results.



Gianluca Sartori
--
https://dueuno.com

Reply via email to