{"record":{"id":"0d173893c82d48ab","repo":"mckaywrigley/chatbot-ui","slug":"error-message-0d1738","errorCode":null,"errorMessage":"error.message","messagePattern":"error\\.message","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"db/collection-files.ts","lineNumber":20,"sourceCode":"import { TablesInsert } from \"@/supabase/types\"\n\nexport const getCollectionFilesByCollectionId = async (\n  collectionId: string\n) => {\n  const { data: collectionFiles, error } = await supabase\n    .from(\"collections\")\n    .select(\n      `\n        id, \n        name, \n        files ( id, name, type )\n      `\n    )\n    .eq(\"id\", collectionId)\n    .single()\n\n  if (!collectionFiles) {\n    throw new Error(error.message)\n  }\n\n  return collectionFiles\n}\n\nexport const createCollectionFile = async (\n  collectionFile: TablesInsert<\"collection_files\">\n) => {\n  const { data: createdCollectionFile, error } = await supabase\n    .from(\"collection_files\")\n    .insert(collectionFile)\n    .select(\"*\")\n\n  if (!createdCollectionFile) {\n    throw new Error(error.message)\n  }\n\n  return createdCollectionFile","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/mckaywrigley/chatbot-ui/blob/81328b61d2a4ab597a7a057be70e785cf756d9f8/db/collection-files.ts#L2-L38","documentation":"Selecting a collection plus its files with .single() errored. Besides transport/permission failures, .single() fails with PGRST116 when zero rows match (collection not found or hidden by RLS) or when the join produces more than one row.","triggerScenarios":"Calling getCollectionFilesByCollectionId with a non-existent collectionId, an id hidden by row level security, or a one-to-many join shape that makes the single-row select ambiguous.","commonSituations":"Deep-linking to a deleted collection, accessing another user's collection with per-user RLS, or changing the select string to include a to-many relation while keeping .single().","solutions":["Use .maybeSingle() if a missing collection is a normal case and return null","Distinguish PGRST116 from real query errors in the handler","Verify collectionId is a valid UUID and visible to the current role","Review the select string's relation cardinality (use nested array syntax for one-to-many)"],"exampleFix":"// before\n.single()\nif (!collectionFiles) {\n  throw new Error(error.message)\n}\n// after\n.maybeSingle()\nif (error) {\n  throw new Error(error.message)\n}\nreturn collectionFiles // may be null for missing collection","handlingStrategy":"type-guard","validationCode":"if (!collectionId) throw new Error(\"collectionId required\");","typeGuard":"const hasFiles = (c: unknown): c is { files: unknown[] } =>\n  typeof c === \"object\" && c !== null && Array.isArray((c as any).files);","tryCatchPattern":"try { return await getCollectionFilesByCollectionId(collectionId) } catch (e) { if (e instanceof Error && e.message.includes(\"PGRST116\")) return null; throw e }","preventionTips":["Prefer .maybeSingle() for lookups where not-found is normal","Check relation cardinality in select strings","Return null for missing collections in route handlers"],"tags":["supabase","postgrest","select","single","collection-files","pgrst116"],"backgroundTag":"supabase-query-not-found","analyzedSha":"81328b61d2a4ab597a7a057be70e785cf756d9f8","analyzedAt":"2026-08-27T19:24:42.689Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}