Add login.avatar_id FK for selected avatar#331
Conversation
Adds a nullable foreign key on the login table referencing avatars_list, making "currently selected avatar" a single authoritative value per user. The legacy avatars.selected flag is kept (and still writable) for backwards compatibility; the API mirrors both sides on writes and provides a reconciler to repair drift. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
📝 WalkthroughWalkthroughMigration V143 adds a nullable Changeslogin.avatar_id Foreign Key Migration
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@migrations/V143__login_avatar_fk.sql`:
- Around line 13-15: The UPDATE statement directly joins on avatars.selected = 1
without handling cases where a user has multiple selected avatars, resulting in
non-deterministic assignment of avatar_id. Refactor the UPDATE to use a subquery
that selects a single avatar per user using a deterministic rule (such as
MAX(idAvatar)) when multiple selected=1 rows exist for the same idUser. This
ensures that each user gets exactly one avatar_id assigned based on a consistent
tie-breaking rule rather than an arbitrary result from the JOIN.
- Around line 7-10: The avatar_id column definition uses mediumint(8) unsigned
but the referenced avatars_list.id column uses int(11) unsigned, causing a type
mismatch that will prevent the foreign key constraint creation. Change the
avatar_id column type from mediumint(8) unsigned to int(11) unsigned to match
the base type and size of the referenced primary key column in avatars_list.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: b7fecc71-991c-4f47-b763-c9f4d25ff1ad
📒 Files selected for processing (1)
migrations/V143__login_avatar_fk.sql
avatars_list.id is int(11) unsigned, not mediumint — the mismatch caused errno 150 "Foreign key constraint is incorrectly formed". Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Partial solution of #102
Summary
avatar_idcolumn onloginwith FK toavatars_list(id)(ON DELETE SET NULL, ON UPDATE CASCADE).login.avatar_idfrom existingavatars.selected = 1rows.avatars.selectedflag is intentionally kept and remains user-writable for backwards compatibility. The faf-java-api change (separate PR) mirrors both columns on writes and exposes a reconciler to repair drift.Test plan
login.avatar_idis populated for every user that previously had exactly oneavatars.selected = 1row.avatar_id IS NULL.avatars_listrow sets the referencinglogin.avatar_idto NULL.🤖 Generated with Claude Code
Summary by CodeRabbit