Debugging a Database Query From the Outside In
Database errors become much easier when you stop changing code randomly and follow the failure through each boundary.
The starting point
A database error can appear in a page component even when the real problem is a stopped service, an incorrect connection string, a missing table, a migration mismatch, or a malformed query. The stack trace shows where the failure surfaced, not necessarily where the underlying assumption became false. Good debugging starts by reconstructing the path from the expected behavior to the actual behavior.
Find the earliest broken boundary
If a page calls a query function and the query fails, first establish whether the database server is reachable. If it is reachable, establish which database the application connected to. If the connection works, inspect whether the expected tables exist. Only after those assumptions hold does it make sense to spend time on the SQL itself.
This ordering prevents a common debugging failure mode: changing correct application code to compensate for a problem somewhere else. A database can be online while the wrong database is selected. A migration can be successful while the local database is stale. Evidence should narrow the search.
Use logs as evidence
A good debugging session records what should happen, what actually happens, and what evidence supports the current hypothesis. That makes it possible to discard a hypothesis when a command contradicts it. Without that discipline, debugging becomes a sequence of guesses that happen to change several variables at once.
The goal is not merely to make the error disappear. It is to identify the earliest meaningful divergence and understand why it occurred so that the same class of failure becomes easier to recognize next time.


OPEN
Thoughts on the article.