Skip to content

raise "duplicate key error" as IntegrityError #139

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

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ jobs:
expressions_case
defer
defer_regress
force_insert_update
from_db_value
generic_relations
generic_relations_regress
Expand Down
6 changes: 5 additions & 1 deletion django_mongodb/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
from django.db.models.sql.constants import INNER
from django.db.models.sql.datastructures import Join
from django.db.models.sql.where import AND, OR, XOR, ExtraWhere, NothingNode, WhereNode
from pymongo.errors import DuplicateKeyError, PyMongoError
from pymongo.errors import BulkWriteError, DuplicateKeyError, PyMongoError


def wrap_database_errors(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except BulkWriteError as e:
if "E11000 duplicate key error" in str(e):
raise IntegrityError from e
raise
except DuplicateKeyError as e:
raise IntegrityError from e
except PyMongoError as e:
Expand Down