fix(db): re-accumulate pending changes by custom getKey to avoid DuplicateKeySyncError#1290
Merged
fix(db): re-accumulate pending changes by custom getKey to avoid DuplicateKeySyncError#1290
Conversation
🦋 Changeset detectedLatest commit: 12c6ca8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 12 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
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: |
Contributor
|
Size Change: +109 B (+0.12%) Total Size: 92.6 kB
ℹ️ View Unchanged
|
Contributor
|
Size Change: 0 B Total Size: 3.7 kB ℹ️ View Unchanged
|
…icateKeySyncError When a LEFT JOIN live query has a custom getKey and the right collection is populated after initial sync, the IVM retracts old rows and inserts new rows with different D2 internal keys that map to the same custom key. Because pendingChanges is keyed by D2 key, these end up as separate entries. If the INSERT is processed first, the sync layer throws DuplicateKeySyncError. Fix by re-accumulating pendingChanges by custom key in flushPendingChanges when this.config.getKey is set. This merges entries that the IVM sees as distinct D2 rows but the user considers the same logical row, producing a single UPDATE instead of a conflicting INSERT + DELETE. Closes #677 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
samwillis
approved these changes
Feb 23, 2026
| // to the same user-visible key. Re-accumulate by custom key so that a | ||
| // retract + insert for the same logical row merges into an UPDATE | ||
| // instead of a separate DELETE and INSERT that can race. | ||
| if (this.config.getKey) { |
Collaborator
There was a problem hiding this comment.
Good that this is only used when there is a getKey 👍
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
getKeythat only considers the left collection's identity (ignoring the right collection, e.g. because it knows it's a 1-to-1 relation), populating the right collection causes the IVM to retract old rows and insert new rows with different D2 internal keys that map to the same custom key. BecausependingChangesis keyed by D2 key, these appear as a separate INSERT and DELETE. If the INSERT is processed first, the sync layer throwsDuplicateKeySyncError.pendingChangesby custom key influshPendingChanges, merging the retract + insert for the same custom key into a single UPDATE. This does not change how new rows are inserted — it only applies when the same custom key appears in both a delete and an insert within the same flush.Test plan
left join with custom getKey should not throw DuplicateKeySyncError)autoIndex: offandautoIndex: eagervariants passCloses #677
🤖 Generated with Claude Code