fix: support associations without an id - #6
Conversation
`maybe_remove_from_association/4` dereferenced `.id` on every member of a loaded has_many, which raises a KeyError for join style schemas with a composite primary key. The filter ran even when nothing was being removed, so any sync message crashed a LiveView with such an association loaded. Compare records using `LiveSync.lookup_info/1` instead, which is also what keys the updates map. Schemas without a `Watch` impl look up as `nil` and are never removed. `sync/4` had the same assumption baked in and would raise for any schema deriving `Watch` with a custom `id` option. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
|
ⓘ Qodo reviews are paused because your workspace is out of credits. Ask your workspace admin to add credits to resume reviews. Manage billing |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThe change adds an ID-less ChangesID-less association synchronization
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant Test
participant LivePage
participant LiveSync
participant Database
Test->>Database: Create ExampleTag
LivePage->>Database: Preload example_tags
LivePage->>Test: Render tag name
Test->>LiveSync: Update parent Example
LiveSync->>LiveSync: Compare lookup_info values
LiveSync->>Test: Synchronize updated parent and tag
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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 `@test/support/live_page.ex`:
- Line 26: Update the preload list in sync/3 to include the :ignored association
alongside :parent, :children, and :example_tags, ensuring render/1 can safely
enumerate `@data.ignored` when it is not already loaded.
🪄 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: Organization UI
Review profile: ASSERTIVE
Plan: Pro
Run ID: 17959656-9051-43e9-b8ee-eca879c2093d
📒 Files selected for processing (8)
lib/live_sync/replication_test.exslib/live_sync/socket.exlib/live_sync/socket_test.exslib/test_helper.exsmix.exstest/support/example.extest/support/example_tag.extest/support/live_page.ex
Already loaded associations survive the traversal, so the test page does not need to preload them again. Not re-preloading also makes the regression test stronger: it now proves the composite primary key association is carried through traverse_associations/3 rather than being reloaded after it. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Problem
maybe_remove_from_association/4dereferenced.idon every member of a loadedhas_many:That raises a
KeyErrorfor join style schemas with a composite primary key and noidfield. Worse, the filter runs even whenrecords_to_removeis empty, so any sync message crashes a LiveView that has such an association loaded — unrelated inserts, updates, or deletes on some other table are enough.Real crash from a LiveView watching a record whose child has a composite primary key:
Fix
Compare records with
LiveSync.lookup_info/1— the same lookup that already keys theupdatesmap — instead of raw.id. Schemas with noWatchimpl look up asniland are never removed, which is the correct behavior for a child schema that isn't synced.sync/4had the same assumption baked in (record.__struct__ == schema and record.id == id) and would raise for any schema derivingWatchwith a customid:option, so it uses the lookup too.Tests
New
works with associations without an idreproduces the stack frame above, via aLiveSync.ExampleTagsupport schema with a composite primary key. Verified red before the fix, green after.🤖 Generated with Claude Code
Summary by CodeRabbit
Bug Fixes
New Features
Tests