Skip to content

[Week7 - hjy] Task3: Add Tag model with many-to-many Note relationship#59

Open
HyungjoonYang wants to merge 1 commit into
mainfrom
week7-hjy-task3
Open

[Week7 - hjy] Task3: Add Tag model with many-to-many Note relationship#59
HyungjoonYang wants to merge 1 commit into
mainfrom
week7-hjy-task3

Conversation

@HyungjoonYang

Copy link
Copy Markdown

Summary

  • Added Tag model (id, name String(50) unique, timestamps) and note_tags association table
  • Configured many-to-many relationship: Note.tagsTag.notes with lazy="selectin" and CASCADE deletes
  • Added TagCreate, TagRead schemas; updated NoteRead to include tags: list[TagRead] = []
  • New tags router: GET /tags/, POST /tags/ (201, 409 on duplicate), DELETE /tags/{tag_id} (204, 404)
  • Added note-tag endpoints: POST /notes/{note_id}/tags/{tag_id} (idempotent attach), DELETE /notes/{note_id}/tags/{tag_id} (detach)
  • Registered tags router in main.py

Test plan

  • Create and list tags
  • Duplicate tag name → 409
  • Delete tag → 204, nonexistent → 404
  • Attach tag to note, verify in response
  • Attach tag twice → idempotent (single tag)
  • Detach tag from note
  • 404 for nonexistent note/tag on attach
  • GET note includes empty tags by default
  • Cascade: deleting tag removes from note associations
  • All 15 tests pass

Notable tradeoffs

  • lazy="selectin" eagerly loads tags with notes (avoids N+1 but adds overhead for tag-less queries)
  • Tag attach is idempotent; detach silently succeeds if not attached
  • 409 Conflict for duplicate tags uses SQLAlchemy IntegrityError catch
  • No tag rename endpoint — delete + recreate works for now

🤖 Generated with Claude Code

Add Tag model (unique name, timestamps) and note_tags association table.
Configure many-to-many relationship with CASCADE deletes and lazy=selectin.
Add tags router (CRUD) and note tag attach/detach endpoints.
Update NoteRead to include tags. 12 new tag tests.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant