fakedata.db). This demonstrates a simple repository pattern with FastAPI, how to keep endpoints thin, and common pitfalls to watch for (file paths, imports, and SQL parameter bindings). It also shows how GitHub Copilot can speed refactors — but you should review structural suggestions before applying them.
We began with an existing FastAPI endpoint that used Faker:

- Add a repository module to encapsulate DB operations and SQL queries.
- Replace Faker-based generation in the endpoint with a call to the repository.
- Keep the FastAPI endpoint focused on request validation and delegation.
- Troubleshoot common issues: DB path, relative imports, and SQL parameter binding.
data/fake_data_repository.py
This module contains the DB connection helper and the query that returns randomly sampled rows. It returns rows as dictionaries.- Use an explicit
DB_PATHso it’s easy to change iffakedata.dblives elsewhere. - Use parameterized queries with
?placeholders to avoid SQL injection and binding issues. - Convert
sqlite3.Rowtodictbefore returning for JSON serialization.
api/endpoints/router.py
Keep the endpoint small: validate the request via Pydantic, call the repository, and return results.src is on the import path) or adjusting PYTHONPATH may be necessary. If you run into import errors such as “ModuleNotFoundError: No module named ‘src’” or “attempted relative import beyond top-level package”, prefer imports that match how you run the application (absolute imports from the package root are usually clearer).
api/models/fake_data_request.py
Simplify the request model to match the endpoint’s contract (we’re only usingcount now).
count (and optionally locale) so FastAPI doesn’t return a 422 Unprocessable Entity.
Example request body:
Troubleshooting highlights and fixes
Common issues are file path mismatches and import errors. Verify that
DB_PATH points to the actual fakedata.db file and that your Python import paths match how you run the server (e.g., run from src or adjust PYTHONPATH).-
Relative import problems
- Error:
ImportError: attempted relative import beyond top-level packageorModuleNotFoundError: No module named 'src'. - Fix: Use imports consistent with how you launch the app. For a layout like
src/api/...and starting the server fromsrc, preferfrom api.models.fake_data_request import FakeDataRequestandfrom data.fake_data_repository import get_fake_data. Alternatively, runpython -mfrom the project root or setPYTHONPATHappropriately.
- Error:
-
Database file location
- Error:
Database error: no such table: fake_data - Fix: Verify
fakedata.dbexists at the path your code expects. DB Browser can open a copy of that file in a different folder — confirm the absolute path or move the DB file.
- Error:
-
SQL parameter binding
- Error:
Incorrect number of bindings supplied. The current statement uses ?, and there are 0 supplied. - Cause: Calling
cursor.execute(query)when query has?placeholders requires passing a tuple/list of parameters. Usecursor.execute(query, (count,)). - If you remove LIMIT temporarily and still use parameter binding, update the call accordingly.
- Error:
-
Debugging tip
- Print or inspect the DB schema: execute
PRAGMA table_info(fake_data);andSELECT COUNT(*) FROM fake_data;to validate columns and row count. Example quick script:
- Print or inspect the DB schema: execute
Lessons learned
- GitHub Copilot can accelerate refactors (for example, creating a repository module and replacing Faker usage with DB access), but it may also propose new files, folders, or import styles that don’t match your project layout. Review suggestions before applying them.
- Keep FastAPI endpoints thin — move DB and business logic into a repository or service layer for testability and clarity.
- With local SQLite files, be explicit about the DB path and run the server from a location that matches your import layout.
- Make small, iterative changes and test frequently. Errors encountered while developing (database or import errors) are useful signals that your new code paths are exercised.
Tip: Use GitHub Copilot to suggest code and comments, but keep control over structure and imports. Small, deliberate edits reduce the chance of the assistant creating mismatched files or confusing folder structures.
Next steps
- Add in-code documentation and unit tests for
data/fake_data_repository.py. - Expand the repository to optionally return specific fields (e.g., only
nameoremail) for a granulardata_typefilter. - Consider adding pagination or cursor-based retrieval for large datasets.
- FastAPI: https://fastapi.tiangolo.com/
- SQLite: https://www.sqlite.org/
- DB Browser for SQLite: https://sqlitebrowser.org/
- Pydantic: https://docs.pydantic.dev/
- GitHub Copilot: https://github.com/features/copilot