Skip to content

Conversation

anivar
Copy link
Contributor

@anivar anivar commented Sep 26, 2025

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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

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