ErrLookupBackground articles › Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first

Database query failed: Internal Server Error 500s wrapping SQL, Prisma, and connection failures — what to check first

Database query failed errors are 500s and wrapped exceptions that occur when a library's SQL query — Prisma, pg, database/sql, or an ORM — throws at runtime: the database is unreachable, locked, schema-drifted, or out of connections. Developers hit them as generic 'Internal Server Error' bodies, 'failed to build ... snapshot' responses, or wrapped messages like 'load wisp labels: %w', while the real database exception lives only in server logs. This page explains the shared mechanism, the most common causes, and the fixes that hold across libraries.

Distilled from 107 documented records across 16 repositories.

Background

This family forms wherever application code calls into a database layer and lets the failure propagate instead of handling it: a Prisma query over the workspaces table in AnythingLLM's API handlers, a pg pool query in worldmonitor's snapshot builders, a database/sql EXISTS probe in beads, a Prisma query_raw call in litellm, or a Django model query in label-studio. The pattern is the same at every layer: the query engine (Prisma's generated client, node-postgres Pool, Go's database/sql, Django ORM) raises a driver-level exception, the route's catch-all or a wrapper converts it to a generic response, and the specific cause — SQLITE_BUSY, missing table, connection refused, statement timeout — is only preserved server-side, in a console.error, fastify.log.error, verbose_proxy_logger traceback, or an appended {e} in the message text.

From the caller's side these errors are deliberately opaque. AnythingLLM returns a bare 'Internal Server Error' whose HTTP body carries no detail; worldmonitor's routes return fixed strings like 'failed to build overview snapshot' while fastify.log.error holds the real exception; litellm converts non-ManagementProblem exceptions into an opaque RFC 9457 problem with a stable urn and logs the traceback in the proxy. Some libraries are more transparent: beads wraps with %w so errors.Is/errors.As can unwrap driver.ErrBadConn or context.DeadlineExceeded, litellm's get_table_info chains __cause__, and siyuan appends the underlying SQL error text directly to 'get block failed: %s'. In every case the debugging move is the same — get to the underlying database error before touching client code.

Within the family, the trigger distribution varies by database backend. SQLite-backed projects (AnythingLLM, siyuan, claude-mem's local store) see lock contention — SQLITE_BUSY from concurrent writers, two server instances on one storage directory, backup jobs holding the file — plus corrupted or missing database files and schema drift when the app is upgraded without its boot-time migrations. Postgres-backed projects (worldmonitor, claude-mem's ingestion, litellm, nautilus_trader) see connection failures (DATABASE_URL unset, connect timeouts, pool exhaustion at max connections), statement timeouts, and failover resets. Dolt-backed beads adds schema and system-table concerns (dolt_remote_branches missing on old versions, migrations not applied).

A recurring design question in these records is fail-open versus fail-closed. Some paths deliberately degrade: worldmonitor serves cached snapshots where possible, claude-mem stores events unlinked when session lookup fails and repairs with a backfill, beads suppresses missing-table errors for wisp labels, and AnythingLLM's isOnboardingComplete returns false on inner failure. Others fail hard on purpose: beads' sweep contract requires aborting rather than under-scanning, so a reference-scan query error stops the prune entirely. Whether a given 500 is safe to retry depends on which side the library chose — read-only queries and keyset pages are generally idempotent, while state transitions and writes need idempotency checks before retry.

Common causes

What usually fixes it

Go deeper

Documented occurrences

…and 87 more across the corpus — use search.

Honest provenance: generated on 2026-08-30 from AI-assisted analysis of the linked records. See how records are made.