- Start: existing FastAPI endpoint used the Faker library to produce fake values.
- Goal: read rows from a local SQLite database (
fakedata.db) and return full records: first name, last name, email, age, city, occupation. - Key troubleshooting items fixed:
- Database file path (working directory differences).
- Connection lifecycle (closing connections too early).
- Correct binding of LIMIT in SQLite queries.
- Relative import/module path issues in package layouts.
- Architecture: a small repository module handles DB access while a thin router calls the repository.
Use tools like GitHub Copilot to speed development, but always validate generated code. Pay special attention to imports, file layout, DB paths, and the connection lifecycle.
- Deterministic test data: a small SQLite DB produces repeatable records (useful for tests and demos).
- Realism: returns full records matching your schema, rather than single fake attributes.
- Education: demonstrates repository patterns, SQL parameter binding, correct connection handling.
- This module contains all DB logic: creating a connection and retrieving records.
- Important: do not close the connection in
get_db_connection()before returning it. Close connections after use in the caller (or in finally blocks inside repository functions).
- Keep the endpoint thin and delegate DB interactions to the repository.
- The request model is a simple Pydantic model containing just
count(and an optionallocaleretained for compatibility).
- After switching to full-record retrieval,
data_typewas no longer needed. Keeplocaleoptional for backward compatibility.
Diagnostic SQL and verification
- Use the sqlite CLI or DB Browser for SQLite to confirm schema:
PRAGMA table_info(fake_data);
- Example schema verification output:
- Print the SQL and DB path before executing to confirm behavior:
- Run the printed SQL directly in DB Browser or the
sqlite3CLI to validate results. - From your application, run
PRAGMA table_info(fake_data);to verify the schema accessible to your running process.
- If you hit
ModuleNotFoundErrorfor top-level package names likesrc, prefer relative imports or ensure the working directory/PYTHONPATH is set to the project root containingapi/anddata/. - The examples above use
api.models...anddata.fake_data_repositoryassuming you run the app from the project root.
Automatic code tools (e.g., GitHub Copilot) can speed development, but they may:
- Introduce unexpected file-layout or import changes.
- Create new files in the wrong locations if the tool lacks full project context.
- Generate code that looks correct but has runtime issues (e.g., closing connections too early, wrong DB path). Always review generated suggestions, run tests, and validate runtime behavior.
- Replaced a Faker-based generator with a DB-backed repository that returns real rows.
- Implemented a clean separation: thin router -> repository -> DB.
- Resolved DB location, parameter binding, and connection lifecycle issues.
- Documented troubleshooting steps to help diagnose similar issues quickly.
- Add query parameters to filter by fields (e.g., age range, city).
- Implement pagination instead of relying on
ORDER BY RANDOM() LIMIT ?for large tables. - Consider caching or a lightweight connection pool for heavier workloads (SQLite is best for small datasets/testing).
- This lesson demonstrates wiring a FastAPI endpoint to a local SQLite database, covering common pitfalls and fixes.
- Use automation tools to assist, but validate structure, imports, and runtime behavior yourself.