Skip to content

Conversation

@ibarakaiev
Copy link
Contributor

When multiple subset queries return the same row (e.g., different WHERE clauses that both match the same record), the server sends insert operations for each response. If there's a race condition and the row's data changed between requests (e.g., timestamp field updated), this caused a DuplicateKeySyncError because TanStack DB's sync layer throws when inserting an existing key with a different value.

This fix tracks synced keys in the Electric adapter and converts subsequent insert operations to update for keys that have already been synced. The tracked keys are cleared on truncate/must-refetch to stay in sync with the collection state.

Note: this might be against the original assumptions of the codebase, so perhaps there's a more reliable way to fix this instead of converting insert to update. Making this PR as a starting point.

🤖 Generated with Claude Code

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

…ent values

When multiple subset queries return the same row (e.g., different WHERE
clauses that both match the same record), the server sends `insert`
operations for each response. If the row's data changed between requests
(e.g., timestamp field updated), this caused a DuplicateKeySyncError
because TanStack DB's sync layer throws when inserting an existing key
with a different value.

This fix tracks synced keys in the Electric adapter and converts
subsequent `insert` operations to `update` for keys that have already
been synced. The tracked keys are cleared on truncate/must-refetch to
stay in sync with the collection state.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@changeset-bot
Copy link

changeset-bot bot commented Dec 23, 2025

🦋 Changeset detected

Latest commit: bf94daf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@tanstack/electric-db-collection Patch

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

@pkg-pr-new
Copy link

pkg-pr-new bot commented Dec 23, 2025

More templates

@tanstack/angular-db

npm i https://pkg.pr.new/@tanstack/angular-db@1070

@tanstack/db

npm i https://pkg.pr.new/@tanstack/db@1070

@tanstack/db-ivm

npm i https://pkg.pr.new/@tanstack/db-ivm@1070

@tanstack/electric-db-collection

npm i https://pkg.pr.new/@tanstack/electric-db-collection@1070

@tanstack/offline-transactions

npm i https://pkg.pr.new/@tanstack/offline-transactions@1070

@tanstack/powersync-db-collection

npm i https://pkg.pr.new/@tanstack/powersync-db-collection@1070

@tanstack/query-db-collection

npm i https://pkg.pr.new/@tanstack/query-db-collection@1070

@tanstack/react-db

npm i https://pkg.pr.new/@tanstack/react-db@1070

@tanstack/rxdb-db-collection

npm i https://pkg.pr.new/@tanstack/rxdb-db-collection@1070

@tanstack/solid-db

npm i https://pkg.pr.new/@tanstack/solid-db@1070

@tanstack/svelte-db

npm i https://pkg.pr.new/@tanstack/svelte-db@1070

@tanstack/trailbase-db-collection

npm i https://pkg.pr.new/@tanstack/trailbase-db-collection@1070

@tanstack/vue-db

npm i https://pkg.pr.new/@tanstack/vue-db@1070

commit: bf94daf

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