A backend API project built with FastAPI, PostgreSQL, SQLAlchemy ORM and Alembic.
This project was created as a practice project for learning how to build a real backend with:
- CRUD operations
- relational database design
- one-to-many and many-to-many relationships
- database migrations
- clean project structure
-
Users
- create, read, update and delete users
- get all notes for a specific user
-
Notes
- create, read, update and delete notes
- get a single note by ID
- filter notes by tag
-
Tags
- create, read, update and delete tags
- get all tags for a specific note
- get all notes for a specific tag
-
NoteTag
- create, read, update and delete note-tag relationships
-
Other
- centralized exception handling
- API route refactoring with
APIRouter - PostgreSQL database migrations with Alembic
- Python 3.12
- FastAPI
- PostgreSQL
- SQLAlchemy
- Alembic
- Pydantic
- Poetry
src/knowledge_base_api/
│
├── main.py
├── database.py
├── models.py
├── schemas.py
├── services.py
├── exceptions.py
└── routes/
├── users.py
├── notes.py
├── tags.py
└── note_tags.py
- One User can have many Notes
- One Note can have many Tags
- One Tag can belong to many Notes
This is implemented through an association table:
note_tags
GET /health
POST /usersGET /usersGET /users/{user_id}PUT /users/{user_id}DELETE /users/{user_id}GET /users/{user_id}/notes
POST /notesGET /notesGET /notes/{note_id}PUT /notes/{note_id}DELETE /notes/{note_id}
Optional filtering:
GET /notes?tag_name=python
POST /tagsGET /tagsPUT /tags/{tag_id}DELETE /tags/{tag_id}GET /notes/{note_id}/tagsGET /tags/{tag_id}/notes
POST /note_tagsGET /note_tagsPUT /note_tags/{note_tag_id}DELETE /note_tags/{note_tag_id}
Clone the repository:
git clone <your-repo-url>
cd knowledge_base_api
Install dependencies:
poetry install
Create a PostgreSQL database manually, for example:
CREATE DATABASE knowledge_base;
Update your database connection in database.py and alembic.ini.
Example:
DATABASE_URL = "postgresql://postgres:your_password@localhost:5432/knowledge_base"sqlalchemy.url = postgresql://postgres:your_password@localhost:5432/knowledge_base
poetry run alembic upgrade head
poetry run uvicorn src.knowledge_base_api.main:app --reload
Swagger UI is available at:
http://127.0.0.1:8000/docs
This project was built to practice:
- designing relational databases
- working with SQLAlchemy relationships
- using Alembic migrations
- structuring a FastAPI backend project
- separating routes, services and database logic
- handling errors in a centralized way