fix: repeat subquery in ORDER BY instead of using alias (PostgreSQL doesn't resolve SELECT aliases inside COALESCE)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Ai
2026-05-22 13:10:42 +03:00
parent c1cf15a67c
commit 0ee3ead826

View File

@@ -44,7 +44,10 @@ export default async function chatRoutes(app: FastifyInstance) {
) END AS private_user_id
FROM chats c
JOIN chat_members cm ON cm.chat_id = c.id AND cm.user_id = $1
ORDER BY COALESCE(last_message_at, c.created_at) DESC
ORDER BY COALESCE(
(SELECT created_at FROM messages WHERE chat_id = c.id AND is_deleted = FALSE ORDER BY created_at DESC LIMIT 1),
c.created_at
) DESC
`, [userId]);
return rows.map(r => ({