Skip to content

Commit

Permalink
raise "duplicate key error" as IntegrityError
Browse files Browse the repository at this point in the history
  • Loading branch information
timgraham committed Sep 27, 2024
1 parent 0a419a1 commit 5b92640
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 0 additions & 4 deletions django_mongodb/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
# subclasses of BaseDatabaseIntrospection may require a get_constraints() method
"migrations.test_operations.OperationTests.test_add_func_unique_constraint",
"migrations.test_operations.OperationTests.test_remove_func_unique_constraint",
# MongoDB's "duplicate key error" must be raised as IntegrityError, not
# DatabaseError.
"force_insert_update.tests.ForceInsertInheritanceTests.test_force_insert_diamond_mti",
"force_insert_update.tests.ForceTests.test_force_update",
}
# $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3.
_django_test_expected_failures_bitwise = {
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

0 comments on commit 5b92640

Please sign in to comment.