Skip to content

Commit 5b92640

Browse files
committed
raise "duplicate key error" as IntegrityError
1 parent 0a419a1 commit 5b92640

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

django_mongodb/features.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,6 @@ class DatabaseFeatures(BaseDatabaseFeatures):
126126
# subclasses of BaseDatabaseIntrospection may require a get_constraints() method
127127
"migrations.test_operations.OperationTests.test_add_func_unique_constraint",
128128
"migrations.test_operations.OperationTests.test_remove_func_unique_constraint",
129-
# MongoDB's "duplicate key error" must be raised as IntegrityError, not
130-
# DatabaseError.
131-
"force_insert_update.tests.ForceInsertInheritanceTests.test_force_insert_diamond_mti",
132-
"force_insert_update.tests.ForceTests.test_force_update",
133129
}
134130
# $bitAnd, #bitOr, and $bitXor are new in MongoDB 6.3.
135131
_django_test_expected_failures_bitwise = {

django_mongodb/query.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,18 @@
99
from django.db.models.sql.constants import INNER
1010
from django.db.models.sql.datastructures import Join
1111
from django.db.models.sql.where import AND, OR, XOR, ExtraWhere, NothingNode, WhereNode
12-
from pymongo.errors import DuplicateKeyError, PyMongoError
12+
from pymongo.errors import BulkWriteError, DuplicateKeyError, PyMongoError
1313

1414

1515
def wrap_database_errors(func):
1616
@wraps(func)
1717
def wrapper(*args, **kwargs):
1818
try:
1919
return func(*args, **kwargs)
20+
except BulkWriteError as e:
21+
if "E11000 duplicate key error" in str(e):
22+
raise IntegrityError from e
23+
raise
2024
except DuplicateKeyError as e:
2125
raise IntegrityError from e
2226
except PyMongoError as e:

0 commit comments

Comments
 (0)