Skip to content

Fix save() OperationFailure with use_revision=True and keep_nulls=False#1281

Merged
CAPITAINMARVEL merged 2 commits intomainfrom
fix/issue-958-revision-keep-nulls
Mar 1, 2026
Merged

Fix save() OperationFailure with use_revision=True and keep_nulls=False#1281
CAPITAINMARVEL merged 2 commits intomainfrom
fix/issue-958-revision-keep-nulls

Conversation

@roman-right
Copy link
Member

Problem

When a Document has both use_revision=True and keep_nulls=False, calling save() on a new document raises OperationFailure because revision_id (initially None) ends up in both the update pipeline's set and unset operators.

MongoDB rejects updates where the same path appears in both operators.

Fix

  • get_top_level_nones() now excludes revision_id when use_revision=True
  • get_dict() now respects the exclude parameter by subtracting excluded fields from the include set

Both save() and save_changes() call get_top_level_nones(), so the fix covers both code paths.

Tests

  • test_save_with_revision_and_keep_nulls_false - new document insert via save()
  • test_update_with_revision_and_keep_nulls_false - update existing doc via save()

Fixes #958

…ionFailure

When both settings are enabled, revision_id (initially None on new
documents) appears in both \ (from SetRevisionId) and \
(from get_top_level_nones), causing MongoDB to reject the update.

Fix: exclude revision_id from get_top_level_nones when use_revision
is enabled — revision_id is managed independently by SetRevisionId
in the update pipeline. Also ensure get_dict respects the exclude
parameter by subtracting excluded fields from the include set.

Fixes #958
Copy link
Contributor

@CAPITAINMARVEL CAPITAINMARVEL left a comment

Choose a reason for hiding this comment

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

LGTM

@CAPITAINMARVEL CAPITAINMARVEL requested review from a team February 16, 2026 13:09
staticxterm
staticxterm previously approved these changes Feb 19, 2026
@staticxterm staticxterm requested a review from a team February 19, 2026 19:09
Copilot AI review requested due to automatic review settings February 28, 2026 23:40
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review is ineligible. To be eligible to request a review, you need a paid Copilot license, or your organization must enable Copilot code review.

Copy link
Contributor

@CAPITAINMARVEL CAPITAINMARVEL left a comment

Choose a reason for hiding this comment

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

Lgtm

@CAPITAINMARVEL CAPITAINMARVEL merged commit 39222d1 into main Mar 1, 2026
25 checks passed
@CAPITAINMARVEL CAPITAINMARVEL deleted the fix/issue-958-revision-keep-nulls branch March 1, 2026 00:08
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.

[BUG] save() results in OperationFailure if use_revision=True and keep_nulls=False

4 participants