Let's say I have modelA with a ForeignKey on modelB.
In api/modelA/services/modelA.js, I can define:
// [...]
module.exports = {
/**
* Promise to fetch all records
*
* @return {Promise}
*/
find (params, populate) {
params = {
...params,
_sort: 'field_from_model_a:DESC,other_field_model_a:ASC'
}
populate = [
'modelB'
]
return strapi.query('post').find(params, populate)
},
// [...]
This allows me to sort my query and I can also filter deep relations when using
curl -X GET "http://localhost:1337/modelA?modelB.field_from_model_b=example_value
But I can't find how to sort on modelB.some_field.
If I write _sort: 'modelB.field_model_b:ASC', I get an error saying: missing FROM-clause entry for table "modelB"
Indeed, since Strapi release v3.4.2, it appears you can deep sort your queries. Yay !
You can find an example in the tests here of the strapi project.
Here is the syntax (as expected in my initial question):
{
// ...
_sort: 'modelB.field_model_b:ASC'
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With