From 140a1bc375ea0c532fc7d02d5fcf44f2ba7970ec Mon Sep 17 00:00:00 2001 From: Eliezer Steinbock <3090527+elie222@users.noreply.github.com> Date: Tue, 6 Jan 2026 02:11:38 +0200 Subject: [PATCH] fix: wrap DISTINCT ON in subquery to restore createdAt ordering MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PostgreSQL's DISTINCT ON requires ORDER BY to start with the DISTINCT column, so the outer query must re-order by createdAt DESC for proper pagination ordering (most recent threads first). 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .../reply-zero/fetch-trackers.ts | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/apps/web/app/(app)/[emailAccountId]/reply-zero/fetch-trackers.ts b/apps/web/app/(app)/[emailAccountId]/reply-zero/fetch-trackers.ts index ed6ba3d12..da8a0bb16 100644 --- a/apps/web/app/(app)/[emailAccountId]/reply-zero/fetch-trackers.ts +++ b/apps/web/app/(app)/[emailAccountId]/reply-zero/fetch-trackers.ts @@ -25,13 +25,16 @@ export async function getPaginatedThreadTrackers({ const [trackers, total] = await Promise.all([ prisma.$queryRaw` - SELECT DISTINCT ON ("threadId") * - FROM "ThreadTracker" - WHERE "emailAccountId" = ${emailAccountId} - AND "resolved" = false - AND "type" = ${type}::text::"ThreadTrackerType" - ${dateClause} - ORDER BY "threadId", "createdAt" DESC + SELECT * FROM ( + SELECT DISTINCT ON ("threadId") * + FROM "ThreadTracker" + WHERE "emailAccountId" = ${emailAccountId} + AND "resolved" = false + AND "type" = ${type}::text::"ThreadTrackerType" + ${dateClause} + ORDER BY "threadId", "createdAt" DESC + ) AS distinct_threads + ORDER BY "createdAt" DESC LIMIT ${PAGE_SIZE} OFFSET ${skip} `,