diff --git a/django_mongodb/features.py b/django_mongodb/features.py index 21cd6488..c001de11 100644 --- a/django_mongodb/features.py +++ b/django_mongodb/features.py @@ -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 = { diff --git a/django_mongodb/query.py b/django_mongodb/query.py index df2d0d19..4c8b4639 100644 --- a/django_mongodb/query.py +++ b/django_mongodb/query.py @@ -9,7 +9,7 @@ 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): @@ -17,6 +17,10 @@ def wrap_database_errors(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: