-
Notifications
You must be signed in to change notification settings - Fork 145
Fix TanStack DB groupBy duplicate insert errors #1030
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
When processing incremental updates to groupBy queries, the D2 pipeline might emit an insert for an updated aggregate without a corresponding delete in certain edge cases. This caused "already exists" errors in the live query collection. Added a defensive check in applyChanges() that treats inserts as updates when the key already exists, but only for queries without custom getKey (like groupBy). Queries with custom getKey + joins still throw errors as expected to alert users of potential data issues. Added comprehensive test suite for groupBy incremental update scenarios: - Basic incremental updates with same groupBy key - Multiple groups being updated - Sum aggregate with incremental updates - Batch vs incremental processing - Subquery pattern (groupBy used as source for another query) - Rapid sequential inserts - Multiple events in single batch
|
More templates
@tanstack/angular-db
@tanstack/db
@tanstack/db-ivm
@tanstack/electric-db-collection
@tanstack/offline-transactions
@tanstack/powersync-db-collection
@tanstack/query-db-collection
@tanstack/react-db
@tanstack/rxdb-db-collection
@tanstack/solid-db
@tanstack/svelte-db
@tanstack/trailbase-db-collection
@tanstack/vue-db
commit: |
|
Size Change: +35 B (+0.04%) Total Size: 89.5 kB
ℹ️ View Unchanged
|
|
Size Change: 0 B Total Size: 3.35 kB ℹ️ View Unchanged
|
Update @tanstack/query-db-collection from ^1.0.7 to ^1.0.8 in: - examples/react/todo - examples/solid/todo This fixes the sherif check that requires consistent dependency versions across the workspace.
The previous fix in applyChanges was insufficient because the actual error was thrown in the sync layer. This adds a fallback that converts inserts to updates for live queries without custom getKey (like groupBy) when the key already exists, preventing duplicate key errors during incremental updates.
Added a test that directly exercises the sync layer fix by: 1. Setting up a collection with utils[LIVE_QUERY_INTERNAL].hasCustomGetKey = false (simulating a live query without custom getKey, like groupBy) 2. Inserting a document with a specific key 3. Attempting to insert another document with the same key (without delete) Without the fix, this would throw "Cannot insert document with key ... already exists". With the fix, the insert is converted to an update. Also added a test to verify regular collections still throw errors on duplicate inserts.
…plicate-inserts-PvGLM # Conflicts: # examples/react/todo/package.json # examples/solid/todo/package.json # pnpm-lock.yaml
|
#1044 also fixes this! So closing |
When processing incremental updates to groupBy queries, the D2 pipeline might emit an insert for an updated aggregate without a corresponding delete in certain edge cases. This caused "already exists" errors in the live query collection.
Added a defensive check in applyChanges() that treats inserts as updates when the key already exists, but only for queries without custom getKey (like groupBy). Queries with custom getKey + joins still throw errors as expected to alert users of potential data issues.
Added comprehensive test suite for groupBy incremental update scenarios:
🎯 Changes
✅ Checklist
pnpm test:pr.🚀 Release Impact