Skip to content

Conversation

@imsidkg
Copy link

@imsidkg imsidkg commented Sep 11, 2025

What?

This PR addresses a bug where bulk updating documents that include array fields with newly added rows would fail. Specifically:

  • When attempting to add a new row to an array field (e.g., store_statuses in the Users collection) across multiple documents in a single bulk update operation:
    • The update would succeed for the first document.
    • The update would fail for subsequent documents.

Why?

The root cause was that Payload's bulk update operation reused the same temporary client-generated ID for new array rows across all documents being updated.

  • When the first document’s array row was inserted, it used this temporary ID.
  • On subsequent documents, trying to insert another row with the same ID triggered a unique constraint violation, causing the entire bulk update to fail.

How?

The fix is implemented within the updateOperation for collections:

  1. Before the data for each document is passed to updateDocument, a deep copy of the data is made.
  2. For any array fields within this copied data:
    • New rows (identified by their temporary string-based IDs) have their id property removed.
  3. This ensures the database adapter generates fresh, unique IDs for each new array row in every document, preventing unique constraint violations.

Link to reproduction

https://github.com/JesperWe/bulk-update-array.git

Fixes #13736

Watch this

@imsidkg imsidkg changed the title fix(collections): Ensure unique IDs for new array rows in bulk updates fix(core): Ensure unique IDs for new array rows in bulk updates Sep 11, 2025
@denolfe denolfe changed the title fix(core): Ensure unique IDs for new array rows in bulk updates fix: Ensure unique IDs for new array rows in bulk updates Sep 11, 2025
@denolfe denolfe changed the title fix: Ensure unique IDs for new array rows in bulk updates fix: ensure unique IDs for new array rows in bulk updates Sep 11, 2025
@denolfe denolfe requested review from AlessioGr and r1tsuu September 11, 2025 18:25
Copy link
Member

@r1tsuu r1tsuu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The implementation doesn't look right to me. This isn't the place for transforming the data, usually you want to do this in our beforeValidate hook here https://github.com/payloadcms/payload/blob/c7795fa4a1fbc4f5892063b9e978d62dd6bb78a7/packages/payload/src/fields/hooks/beforeValidate/promise.ts or beforeChange could also work as well https://github.com/payloadcms/payload/blob/c7795fa4a1fbc4f5892063b9e978d62dd6bb78a7/packages/payload/src/fields/hooks/beforeChange/promise.ts, also it's not only about the place, but for example if your array field is nested to some another field - this wouldn't work.

2 - we definitely want an integration test here.

@github-actions github-actions bot added the stale label Oct 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bulk update of array with relationship fails

2 participants