Skip to content

Conversation

@veeceey
Copy link

@veeceey veeceey commented Feb 8, 2026

Summary

  • Fixes incorrect exception handling where DuplicateKeyError on unique constraints was being converted to RevisionIdWasChanged
  • Only raises RevisionIdWasChanged when the duplicate key error is actually related to revision_id

Changes

  • Updated exception handler in Document.save() to check if the DuplicateKeyError mentions "revision_id" before converting it
  • Re-raises the original DuplicateKeyError if it's for a different unique constraint

Test plan

  • Verified the logic correctly distinguishes between revision_id conflicts and other unique constraint violations
  • Maintainers can test by creating a Document with a unique indexed field and attempting to save duplicates
  • Expected: DuplicateKeyError is raised (not RevisionIdWasChanged)

Fixes #1057

veeceey and others added 3 commits February 7, 2026 23:07
…eyError

When a Document has a unique indexed attribute, violating the uniqueness
constraint was incorrectly raising RevisionIdWasChanged instead of the
proper DuplicateKeyError.

Updated the exception handler to check if the DuplicateKeyError is
actually related to revision_id before converting it to
RevisionIdWasChanged. If the error is for a different unique constraint,
the original DuplicateKeyError is now re-raised.

Fixes BeanieODM#1057
…tor imports conditional

ConfigDict only exists in pydantic v2, and root_validator is only in pydantic v1.
Move these imports to conditional blocks based on IS_PYDANTIC_V2 to fix import errors
when running tests with pydantic 1.10.18.
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] update() raises a RevisionIdWasChanged when expecting a DuplicateKeyError

1 participant