Skip to content

Commit 2c7280d

Browse files
committed
INTPYTHON-945 Make creating models with expressions raise NotSupportedError
1 parent 91e1755 commit 2c7280d

3 files changed

Lines changed: 25 additions & 15 deletions

File tree

django_mongodb_backend/compiler.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -920,7 +920,12 @@ def execute_sql(self, returning_fields=None):
920920
raise IntegrityError(
921921
f"You can't set {field.name} (a non-nullable field) to None."
922922
)
923-
923+
if hasattr(value, "as_mql"):
924+
raise NotSupportedError(
925+
f"MongoDB does not support creating models with "
926+
f"expressions: got {value} for field {field.name}."
927+
# % You can't set {field.name} (a non-nullable field) to None."
928+
)
924929
field_values[field.column] = value
925930
objs.append(field_values)
926931
return self.insert(objs, returning_fields=returning_fields)

django_mongodb_backend/features.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,6 @@ def django_test_expected_failures(self):
137137
"schema.tests.SchemaTests.test_rename_keep_db_default",
138138
"validation.test_unique.PerformUniqueChecksTest.test_unique_db_default",
139139
},
140-
"Insert expressions aren't supported.": {
141-
"basic.tests.ModelTest.test_save_expressions",
142-
"bulk_create.tests.BulkCreateTests.test_bulk_insert_now",
143-
"bulk_create.tests.BulkCreateTests.test_bulk_insert_expressions",
144-
"expressions.tests.BasicExpressionsTests.test_new_object_create",
145-
"expressions.tests.BasicExpressionsTests.test_new_object_save",
146-
"expressions.tests.BasicExpressionsTests.test_object_create_with_aggregate",
147-
"expressions.tests.BasicExpressionsTests.test_object_create_with_f_expression_in_subquery",
148-
"expressions.tests.BasicExpressionsTests.test_object_update_unsaved_objects",
149-
# PI()
150-
"db_functions.math.test_round.RoundTests.test_decimal_with_precision",
151-
"db_functions.math.test_round.RoundTests.test_float_with_precision",
152-
},
153140
"MongoDB doesn't rename an index when a field is renamed.": {
154141
"migrations.test_operations.OperationTests.test_rename_field_index_together",
155142
"migrations.test_operations.OperationTests.test_rename_field_unique_together",
@@ -480,6 +467,24 @@ def django_test_skips(self):
480467

481468
# Tests that are expected to raise certain exceptions:
482469
_django_test_expected_raises = {
470+
(
471+
NotSupportedError,
472+
"MongoDB does not support creating models with expressions: got "
473+
"Now() for field pub_date.",
474+
): {
475+
"basic.tests.ModelTest.test_save_expressions",
476+
},
477+
(NotSupportedError, "MongoDB does not support creating models with expressions"): {
478+
"bulk_create.tests.BulkCreateTests.test_bulk_insert_now",
479+
"bulk_create.tests.BulkCreateTests.test_bulk_insert_expressions",
480+
"expressions.tests.BasicExpressionsTests.test_new_object_create",
481+
"expressions.tests.BasicExpressionsTests.test_new_object_save",
482+
"expressions.tests.BasicExpressionsTests.test_object_create_with_aggregate",
483+
"expressions.tests.BasicExpressionsTests.test_object_create_with_f_expression_in_subquery",
484+
"expressions.tests.BasicExpressionsTests.test_object_update_unsaved_objects",
485+
"db_functions.math.test_round.RoundTests.test_decimal_with_precision",
486+
"db_functions.math.test_round.RoundTests.test_float_with_precision",
487+
},
483488
(NotSupportedError, "Pattern lookups on UUIDField are not supported."): {
484489
"model_fields.test_uuid.TestQuerying.test_contains",
485490
"model_fields.test_uuid.TestQuerying.test_endswith",

docs/releases/6.0.x.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ New features
1515
Bug fixes
1616
---------
1717

18-
- ...
18+
- Made creating models with expressions raise a helpful error message.
1919

2020
6.0.3
2121
=====

0 commit comments

Comments
 (0)