mckaywrigley/chatbot-ui · error · Error
error.message
Error message
error.message
What it means
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.
Source
Thrown at db/collection-files.ts:20
import { TablesInsert } from "@/supabase/types"
export const getCollectionFilesByCollectionId = async (
collectionId: string
) => {
const { data: collectionFiles, error } = await supabase
.from("collections")
.select(
`
id,
name,
files ( id, name, type )
`
)
.eq("id", collectionId)
.single()
if (!collectionFiles) {
throw new Error(error.message)
}
return collectionFiles
}
export const createCollectionFile = async (
collectionFile: TablesInsert<"collection_files">
) => {
const { data: createdCollectionFile, error } = await supabase
.from("collection_files")
.insert(collectionFile)
.select("*")
if (!createdCollectionFile) {
throw new Error(error.message)
}
return createdCollectionFileView on GitHub (pinned to 81328b61d2)
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)
Example fix
// before
.single()
if (!collectionFiles) {
throw new Error(error.message)
}
// after
.maybeSingle()
if (error) {
throw new Error(error.message)
}
return collectionFiles // may be null for missing collection Defensive patterns
Strategy: type-guard
Validate before calling
if (!collectionId) throw new Error("collectionId required"); Type guard
const hasFiles = (c: unknown): c is { files: unknown[] } =>
typeof c === "object" && c !== null && Array.isArray((c as any).files); Try / catch
try { return await getCollectionFilesByCollectionId(collectionId) } catch (e) { if (e instanceof Error && e.message.includes("PGRST116")) return null; throw e } Prevention
- Prefer .maybeSingle() for lookups where not-found is normal
- Check relation cardinality in select strings
- Return null for missing collections in route handlers
When it happens
Trigger: 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.
Common situations: 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().
Related errors
AI-assisted analysis of mckaywrigley/chatbot-ui@81328b61d2 (2026-08-27).
Data as JSON: /api/errors/0d173893c82d48ab.
Report an issue: GitHub.