{"record":{"id":"8e0a2cb4f6c64374","repo":"mckaywrigley/chatbot-ui","slug":"error-message-8e0a2c","errorCode":null,"errorMessage":"error.message","messagePattern":"error\\.message","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"db/messages.ts","lineNumber":39,"sourceCode":"    .select(\"*\")\n    .eq(\"chat_id\", chatId)\n\n  if (!messages) {\n    throw new Error(\"Messages not found\")\n  }\n\n  return messages\n}\n\nexport const createMessage = async (message: TablesInsert<\"messages\">) => {\n  const { data: createdMessage, error } = await supabase\n    .from(\"messages\")\n    .insert([message])\n    .select(\"*\")\n    .single()\n\n  if (error) {\n    throw new Error(error.message)\n  }\n\n  return createdMessage\n}\n\nexport const createMessages = async (messages: TablesInsert<\"messages\">[]) => {\n  const { data: createdMessages, error } = await supabase\n    .from(\"messages\")\n    .insert(messages)\n    .select(\"*\")\n\n  if (error) {\n    throw new Error(error.message)\n  }\n\n  return createdMessages\n}\n","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/mckaywrigley/chatbot-ui/blob/81328b61d2a4ab597a7a057be70e785cf756d9f8/db/messages.ts#L21-L57","documentation":"Supabase (supabase-js) insert on the 'messages' table returned a PostgREST error instead of a created row. The `.single()` call expects exactly one row back; the error object carries the PostgREST message (constraint violation, RLS denial, missing column, etc.). This is a database-level rejection of the INSERT, not a network failure.","triggerScenarios":"Calling createMessage with a payload that violates a NOT NULL or FOREIGN KEY constraint on 'messages' (e.g. missing user_id or a non-existent chat/thread id), inserting a value exceeding a column's length, or hitting a UNIQUE constraint; also occurs when row-level security policies block the insert for the current anon/auth client.","commonSituations":"RLS enabled on 'messages' without an INSERT policy so anonymous/server-client inserts are silently rejected; passing camelCase keys instead of snake_case column names; environment mismatch where the local schema lacks columns present in the production schema; JWT of a user who doesn't own the referenced chat.","solutions":["Inspect error.message and error.code — PostgREST error codes like 23503 (foreign_key_violation), 23505 (unique_violation), or 42501 (RLS) pinpoint the cause","Verify all required columns in the message payload (check NOT NULL columns and FK targets in the Supabase dashboard Table Editor)","If RLS is enabled on 'messages', add an INSERT policy (e.g. WITH CHECK (auth.uid() = user_id)) or use the service-role client for trusted server-side inserts","Confirm snake_case column names in the payload match the actual schema"],"exampleFix":"// before\nconst { data: createdMessage, error } = await supabase\n  .from(\"messages\")\n  .insert([message])\n  .select(\"*\")\n  .single()\nif (error) {\n  throw new Error(error.message)\n}\n\n// after — surface code + details for diagnosis\nif (error) {\n  throw new Error(`createMessage failed [${error.code}]: ${error.message}`, { cause: error })\n}","handlingStrategy":"validation","validationCode":"// Verify FK targets exist before inserting\nconst { data: chat } = await supabase.from(\"chats\").select(\"id\").eq(\"id\", message.chat_id).maybeSingle()\nif (!chat) throw new Error(`chat ${message.chat_id} does not exist`)\nconst required = [\"chat_id\", \"content\"] // adjust to your NOT NULL columns\nfor (const k of required) {\n  if (message[k] === undefined || message[k] === null) throw new Error(`missing field: ${k}`)\n}","typeGuard":"const isMessageInsert = (m: unknown): m is TablesInsert<\"messages\"> =>\n  typeof m === \"object\" && m !== null && \"chat_id\" in m && \"content\" in m","tryCatchPattern":"try {\n  return await createMessage(message)\n} catch (e) {\n  const msg = e instanceof Error ? e.message : String(e)\n  if (msg.includes(\"duplicate key\")) throw new ConflictError(msg)\n  if (msg.includes(\"violates foreign key\")) throw new BadRequestError(msg)\n  throw e\n}","preventionTips":["Strip undefined keys from payloads before insert","Keep a runtime schema mirror (zod) of NOT NULL columns and validate payloads","Run server-side writes with the service-role client or maintain explicit RLS INSERT policies"],"tags":["supabase","postgres","insert","constraint-violation","rls"],"backgroundTag":"supabase-insert-constraint-violation","analyzedSha":"81328b61d2a4ab597a7a057be70e785cf756d9f8","analyzedAt":"2026-08-27T19:24:42.689Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}