Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

fix(datastore): prevent version cross-contamination between different model types #14570

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

Open
anivar wants to merge 1 commit into aws-amplify:main
base: main
Choose a base branch
Loading
from anivar:fix/datastore-save-version-conflict-13412

Conversation

Copy link
Contributor

@anivar anivar commented Sep 26, 2025
edited
Loading

Fixes issue where DataStore.save() operations on different model types with the same ID would incorrectly share _version values, causing subsequent mutations to fail with version conflicts.

Resolves #13412

Description

The syncOutboxVersionsOnDequeue method was querying mutations by modelId only, without filtering by model type. This caused version synchronization to occur across different model types that happened to share the same primary key value.

This is a minimal fix with 1 line of actual code change that solves a critical production issue affecting multi-model applications.

Problem

When saving different model types consecutively that have the same ID:

  1. UserLevel with ID "123" gets saved → response with _version: 5
  2. IndividualData with ID "123" gets saved
  3. The IndividualData mutation incorrectly receives _version: 5 from the UserLevel response
  4. When IndividualData mutation executes, it fails with version conflict

Root Cause

The predicate in syncOutboxVersionsOnDequeue only filtered by modelId without considering model type, causing version contamination between different models.

Solution

Added model type filter to the predicate to ensure version synchronization only occurs within the same model type:

const predicate = ModelPredicateCreator.createFromAST<MutationEvent>(
 mutationEventModelDefinition,
 {
 and: [
 { model: { eq: head.model } }, // Added this line
 { modelId: { eq: recordId } },
 { id: { ne: this.inProgressMutationEventId } },
 ],
 },
);

Testing

Added test case "Should NOT sync the _version across different model types with the same ID" to verify the fix prevents version cross-contamination while preserving legitimate version synchronization within the same model type.

Changes

  • Modified: packages/datastore/src/sync/outbox.ts - Added model type filter to predicate
  • Added: Test coverage in packages/datastore/tests/outbox.test.ts

Fixes #13412

... model types
Fixes issue where DataStore.save() operations on different model types with the same ID
would incorrectly share _version values, causing subsequent mutations to fail with
version conflicts.
The syncOutboxVersionsOnDequeue method now filters by model type in addition to modelId
to ensure version synchronization only occurs within the same model type.
Fixes aws-amplify#13412 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Datastore.save() sending _version of previous query model instead of current model.

1 participant

AltStyle によって変換されたページ (->オリジナル) /