From f778942630954304c26fcb35ea70b365d807def7 Mon Sep 17 00:00:00 2001 From: Andras Bacsai <5845193+andrasbacsai@users.noreply.github.com> Date: Thu, 29 Oct 2026 00:00:06 +0100 Subject: [PATCH] docs(ai): document hidden counts in application queries --- .ai/lessons.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.ai/lessons.md b/.ai/lessons.md index 3452106a2e..7b3ba6f2be 100644 --- a/.ai/lessons.md +++ b/.ai/lessons.md @@ -155,5 +155,8 @@ ## Validate input safely - In PCRE, `$` also matches before a final `\n`. In validation regexes, use `\z` or reject control characters first, or a trailing newline passes. +## Application queries carry hidden counts +- `Application` has a global scope `withRelations` that adds two `withCount` subqueries and `applications.*`, also to `pluck('id')` and eager loads. For ID lookups and lean eager loads, call `withoutGlobalScope('withRelations')`. + ## Measure database reads end to end - On PostgreSQL 15, `EXPLAIN (ANALYZE, BUFFERS)` does not detoast or send the selected columns, so a wide `jsonb`/`json`/`text` column does not show in it. Time the full fetch (`psql \timing` with `-o /dev/null`, or PDO `fetchAll`) as well before you blame or rule out an index.