Hi! I'm trying to sort a result by an aggregated field (_count) that comes from a related table which should be filtered by some conditions.
It looks like includes runs after orderBy, because my order is not affected by the include ... where condition.
This is the generated query (where true 🤔 ):
LEFT JOIN
(SELECT "public"."related_table"."id",
COUNT(*) AS "orderby_aggregator"
FROM "public"."related_table"
WHERE 1=1
This is the include statement:
include: {
<relatedTable>: {
where: {
<field>: <condition>,
},
}
},
Order by:
orderBy: {
<relatedTable>: {
_count: 'desc',
},
},
_count will include items that are excluded in the include statement ( 😅 ) and affect the orderBy.
Am I missing something? Or my whole idea is wrong? Is there a workaround for this? 😰
Thank you! 🙇♂️
Prisma doesn't directly support sorting by the count of a filtered relation.