Skip to content

[Week7 - pky] Task3: Add Tag model with many-to-many Note relationship#55

Open
kunyoungp wants to merge 1 commit into
mainfrom
week7-pky-task3
Open

[Week7 - pky] Task3: Add Tag model with many-to-many Note relationship#55
kunyoungp wants to merge 1 commit into
mainfrom
week7-pky-task3

Conversation

@kunyoungp

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 tag endpoints on notes: POST /notes/{note_id}/tags/{tag_id} (idempotent attach), DELETE /notes/{note_id}/tags/{tag_id} (detach)
  • Registered tags router in main.py
  • Updated seed.sql with tags and note_tags CREATE TABLE statements

Test plan

  • test_create_and_list_tags — create and list tags
  • test_create_duplicate_tag_returns_409 — duplicate name returns 409
  • test_delete_tag — delete returns 204
  • test_delete_tag_not_found — delete non-existent returns 404
  • test_attach_tag_to_note — attach tag, verify in response
  • test_attach_tag_idempotent — attaching twice results in single tag
  • test_detach_tag_from_note — detach tag, verify removed
  • test_note_read_includes_tags — GET note includes tags field
  • test_cascade_delete_tag_cleans_note_tags — deleting tag removes from note associations
  • All 12 tests pass (make test)

Notable tradeoffs

  • lazy="selectin" eagerly loads tags with notes (avoids N+1 but adds overhead for tag-less queries)
  • Tag attach is idempotent (no error if already attached); detach silently succeeds if not attached
  • 409 Conflict for duplicate tag names uses SQLAlchemy IntegrityError catch rather than pre-check query

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.6 <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