| 298 | 298 | If all sources are None, then an error is raised higher up the stack in |
| 299 | 299 | the output_field property. |
| 300 | 300 | """ |
| 301 | sources_iter = (source for source in self.get_source_fields() if source is not None) | |
| 302 | for output_field in sources_iter: | |
| 303 | for source in sources_iter: | |
| 304 | if not isinstance(output_field, source.__class__): | |
| 305 | raise FieldError( | |
| 306 | 'Expression contains mixed types: %s, %s. You must ' | |
| 307 | 'set output_field.' % ( | |
| 308 | output_field.__class__.__name__, | |
| 309 | source.__class__.__name__, | |
| 310 | ) | |
| 301 | from django.db.models.fields import DateTimeField, DateField, TimeField, DurationField | |
| 302 | ||
| 303 | sources = list(source for source in self.get_source_fields() if source is not None) | |
| 304 | if not sources: | |
| 305 | return None | |
| 306 | ||
| 307 | # Handle temporal arithmetic: DateTimeField - DateTimeField = DurationField | |
| 308 | if (len(sources) == 2 and | |
| 309 | isinstance(sources[0], (DateTimeField, DateField, TimeField)) and | |
| 310 | isinstance(sources[1], (DateTimeField, DateField, TimeField))): | |
| 311 | return DurationField() | |
| 312 | ||
| 313 | # Handle DurationField + DurationField = DurationField | |
| 314 | if all(isinstance(source, DurationField) for source in sources): | |
| 315 | return DurationField() | |
| 316 | ||
| 317 | # Original logic for other cases - check that all sources are same type | |
| 318 | first_type = type(sources[0]) | |
| 319 | for source in sources[1:]: | |
| 320 | if type(source) != first_type: # Use exact type comparison | |
| 321 | raise FieldError( | |
| 322 | 'Expression contains mixed types: %s, %s. You must ' | |
| 323 | 'set output_field.' % ( | |
| 324 | first_type.__name__, | |
| 325 | type(source).__name__, | |
| 311 | 326 | ) |
| 312 | return output_field | |
| 327 | ) | |
| 328 | return sources[0] | |
| 313 | 329 | |
| 314 | 330 | @staticmethod |
| 315 | 331 | def _convert_value_noop(value, expression, connection): |
| 442 | 458 | def set_source_expressions(self, exprs): |
| 443 | 459 | self.lhs, self.rhs = exprs |
| 444 | 460 | |
| 461 | def _resolve_output_field(self): | |
| 462 | """ | |
| 463 | Override to handle temporal arithmetic cases before expressions are resolved. | |
| 464 | This allows DateTimeField - DateTimeField = DurationField to work without | |
| 465 | requiring ExpressionWrapper. | |
| 466 | """ | |
| 467 | from django.db.models.fields import DateTimeField, DateField, TimeField, DurationField | |
| 468 | ||
| 469 | # Try to get the output fields from both sides | |
| 470 | try: | |
| 471 | lhs_field = self.lhs.output_field | |
| 472 | except (FieldError, AttributeError): | |
| 473 | lhs_field = None | |
| 474 | ||
| 475 | try: | |
| 476 | rhs_field = self.rhs.output_field | |
| 477 | except (FieldError, AttributeError): | |
| 478 | rhs_field = None | |
| 479 | ||
| 480 | # Handle temporal subtraction: DateTimeField - DateTimeField = DurationField | |
| 481 | if (self.connector == self.SUB and | |
| 482 | lhs_field is not None and rhs_field is not None and | |
| 483 | type(lhs_field) == type(rhs_field) and # Exact type match required | |
| 484 | isinstance(lhs_field, (DateTimeField, DateField, TimeField))): | |
| 485 | return DurationField() | |
| 486 | ||
| 487 | # Handle DurationField + DurationField = DurationField | |
| 488 | if (lhs_field is not None and rhs_field is not None and | |
| 489 | isinstance(lhs_field, DurationField) and isinstance(rhs_field, DurationField)): | |
| 490 | return DurationField() | |
| 491 | ||
| 492 | # If we can't determine field types, be conservative and don't allow mixed types | |
| 493 | if lhs_field is None or rhs_field is None: | |
| 494 | # Let the parent handle it, but this will likely fail in as_sql | |
| 495 | return super()._resolve_output_field() | |
| 496 | ||
| 497 | # For all other cases, fall back to parent implementation | |
| 498 | return super()._resolve_output_field() | |
| 499 | ||
| 445 | 500 | def as_sql(self, compiler, connection): |
| 446 | 501 | try: |
| 447 | 502 | lhs_type = self.lhs.output_field.get_internal_type() |
| Test Name | Status |
|---|---|
test_date_case_subtraction (expressions.tests.FTimeDeltaTests) | Pass |
test_date_subquery_subtraction (expressions.tests.FTimeDeltaTests) | Pass |
test_date_subtraction (expressions.tests.FTimeDeltaTests) | Pass |
test_datetime_subquery_subtraction (expressions.tests.FTimeDeltaTests) | Pass |
test_datetime_subtraction_microseconds (expressions.tests.FTimeDeltaTests) | Pass |
test_time_subquery_subtraction (expressions.tests.FTimeDeltaTests) | Pass |
test_time_subtraction (expressions.tests.FTimeDeltaTests) | Pass |
test_deconstruct (expressions.tests.FTests) | Pass |
test_deepcopy (expressions.tests.FTests) | Pass |
test_equal (expressions.tests.FTests) | Pass |
test_hash (expressions.tests.FTests) | Pass |
test_not_equal_Value (expressions.tests.FTests) | Pass |
test_and (expressions.tests.CombinableTests) | Pass |
test_negation (expressions.tests.CombinableTests) | Pass |
test_or (expressions.tests.CombinableTests) | Pass |
test_reversed_and (expressions.tests.CombinableTests) | Pass |
test_reversed_or (expressions.tests.CombinableTests) | Pass |
test_empty_group_by (expressions.tests.ExpressionWrapperTests) | Pass |
test_non_empty_group_by (expressions.tests.ExpressionWrapperTests) | Pass |
test_aggregates (expressions.tests.ReprTests) | Pass |
test_distinct_aggregates (expressions.tests.ReprTests) | Pass |
test_expressions (expressions.tests.ReprTests) | Pass |
test_filtered_aggregates (expressions.tests.ReprTests) | Pass |
test_functions (expressions.tests.ReprTests) | Pass |
test_equal (expressions.tests.SimpleExpressionTests) | Pass |
test_hash (expressions.tests.SimpleExpressionTests) | Pass |
test_month_aggregation (expressions.tests.FieldTransformTests) | Pass |
test_multiple_transforms_in_values (expressions.tests.FieldTransformTests) | Pass |
test_transform_in_values (expressions.tests.FieldTransformTests) | Pass |
test_F_reuse (expressions.tests.ExpressionsTests) | Pass |
test_insensitive_patterns_escape (expressions.tests.ExpressionsTests) | Pass |
test_patterns_escape (expressions.tests.ExpressionsTests) | Pass |
test_complex_expressions (expressions.tests.ExpressionsNumericTests) | Pass |
test_fill_with_value_from_same_object (expressions.tests.ExpressionsNumericTests) | Pass |
test_filter_not_equals_other_field (expressions.tests.ExpressionsNumericTests) | Pass |
test_increment_value (expressions.tests.ExpressionsNumericTests) | Pass |
test_deconstruct (expressions.tests.ValueTests) | Pass |
test_deconstruct_output_field (expressions.tests.ValueTests) | Pass |
test_equal (expressions.tests.ValueTests) | Pass |
test_equal_output_field (expressions.tests.ValueTests) | Pass |
test_hash (expressions.tests.ValueTests) | Pass |
test_raise_empty_expressionlist (expressions.tests.ValueTests) | Pass |
test_update_TimeField_using_Value (expressions.tests.ValueTests) | Pass |
test_update_UUIDField_using_Value (expressions.tests.ValueTests) | Pass |
test_complex_expressions_do_not_introduce_sql_injection_via_untrusted_string_inclusion (expressions.tests.IterableLookupInnerExpressionsTests) | Pass |
test_expressions_in_lookups_join_choice (expressions.tests.IterableLookupInnerExpressionsTests) | Pass |
test_in_lookup_allows_F_expressions_and_expressions_for_datetimes (expressions.tests.IterableLookupInnerExpressionsTests) | Pass |
test_in_lookup_allows_F_expressions_and_expressions_for_integers (expressions.tests.IterableLookupInnerExpressionsTests) | Pass |
test_range_lookup_allows_F_expressions_and_expressions_for_integers (expressions.tests.IterableLookupInnerExpressionsTests) | Pass |
test_lefthand_addition (expressions.tests.ExpressionOperatorTests) | Pass |
test_lefthand_bitwise_and (expressions.tests.ExpressionOperatorTests) | Pass |
test_lefthand_bitwise_left_shift_operator (expressions.tests.ExpressionOperatorTests) | Pass |
test_lefthand_bitwise_or (expressions.tests.ExpressionOperatorTests) | Pass |
test_lefthand_bitwise_right_shift_operator (expressions.tests.ExpressionOperatorTests) | Pass |
test_lefthand_bitwise_xor (expressions.tests.ExpressionOperatorTests) | Pass |
test_lefthand_bitwise_xor_null (expressions.tests.ExpressionOperatorTests) | Pass |
test_lefthand_division (expressions.tests.ExpressionOperatorTests) | Pass |
test_lefthand_modulo (expressions.tests.ExpressionOperatorTests) | Pass |
test_lefthand_multiplication (expressions.tests.ExpressionOperatorTests) | Pass |
test_lefthand_power (expressions.tests.ExpressionOperatorTests) | Pass |
test_lefthand_subtraction (expressions.tests.ExpressionOperatorTests) | Pass |
test_right_hand_addition (expressions.tests.ExpressionOperatorTests) | Pass |
test_right_hand_division (expressions.tests.ExpressionOperatorTests) | Pass |
test_right_hand_modulo (expressions.tests.ExpressionOperatorTests) | Pass |
test_right_hand_multiplication (expressions.tests.ExpressionOperatorTests) | Pass |
test_right_hand_subtraction (expressions.tests.ExpressionOperatorTests) | Pass |
test_righthand_power (expressions.tests.ExpressionOperatorTests) | Pass |
test_date_comparison (expressions.tests.FTimeDeltaTests) | Pass |
test_date_minus_duration (expressions.tests.FTimeDeltaTests) | Pass |
test_datetime_subtraction (expressions.tests.FTimeDeltaTests) | Pass |
test_delta_add (expressions.tests.FTimeDeltaTests) | Pass |
test_delta_subtract (expressions.tests.FTimeDeltaTests) | Pass |
test_delta_update (expressions.tests.FTimeDeltaTests) | Pass |
test_duration_expressions (expressions.tests.FTimeDeltaTests) | Pass |
test_duration_with_datetime (expressions.tests.FTimeDeltaTests) | Pass |
test_duration_with_datetime_microseconds (expressions.tests.FTimeDeltaTests) | Pass |
test_durationfield_add (expressions.tests.FTimeDeltaTests) | Pass |
test_exclude (expressions.tests.FTimeDeltaTests) | Pass |
test_invalid_operator (expressions.tests.FTimeDeltaTests) | Pass |
test_mixed_comparisons2 (expressions.tests.FTimeDeltaTests) | Pass |
test_multiple_query_compilation (expressions.tests.FTimeDeltaTests) | Pass |
test_negative_timedelta_update (expressions.tests.FTimeDeltaTests) | Pass |
test_query_clone (expressions.tests.FTimeDeltaTests) | Pass |
test_aggregate_subquery_annotation (expressions.tests.BasicExpressionsTests) | Pass |
test_annotate_values_aggregate (expressions.tests.BasicExpressionsTests) | Pass |
test_annotate_values_count (expressions.tests.BasicExpressionsTests) | Pass |
test_annotate_values_filter (expressions.tests.BasicExpressionsTests) | Pass |
test_annotation_with_nested_outerref (expressions.tests.BasicExpressionsTests) | Pass |
test_annotation_with_outerref (expressions.tests.BasicExpressionsTests) | Pass |
test_annotations_within_subquery (expressions.tests.BasicExpressionsTests) | Pass |
test_arithmetic (expressions.tests.BasicExpressionsTests) | Pass |
test_boolean_expression_combined (expressions.tests.BasicExpressionsTests) | Pass |
test_case_in_filter_if_boolean_output_field (expressions.tests.BasicExpressionsTests) | Pass |
test_exist_single_field_output_field (expressions.tests.BasicExpressionsTests) | Pass |
test_exists_in_filter (expressions.tests.BasicExpressionsTests) | Pass |
test_explicit_output_field (expressions.tests.BasicExpressionsTests) | Pass |
test_filter_inter_attribute (expressions.tests.BasicExpressionsTests) | Pass |
test_filter_with_join (expressions.tests.BasicExpressionsTests) | Pass |
test_filtering_on_annotate_that_uses_q (expressions.tests.BasicExpressionsTests) | Pass |
test_filtering_on_q_that_is_boolean (expressions.tests.BasicExpressionsTests) | Pass |
test_filtering_on_rawsql_that_is_boolean (expressions.tests.BasicExpressionsTests) | Pass |
test_in_subquery (expressions.tests.BasicExpressionsTests) | Pass |
test_incorrect_field_in_F_expression (expressions.tests.BasicExpressionsTests) | Pass |
test_incorrect_joined_field_in_F_expression (expressions.tests.BasicExpressionsTests) | Pass |
test_nested_outerref_with_function (expressions.tests.BasicExpressionsTests) | Pass |
test_nested_subquery (expressions.tests.BasicExpressionsTests) | Pass |
test_nested_subquery_join_outer_ref (expressions.tests.BasicExpressionsTests) | Pass |
test_nested_subquery_outer_ref_2 (expressions.tests.BasicExpressionsTests) | Pass |
test_nested_subquery_outer_ref_with_autofield (expressions.tests.BasicExpressionsTests) | Pass |
test_new_object_create (expressions.tests.BasicExpressionsTests) | Pass |
test_new_object_save (expressions.tests.BasicExpressionsTests) | Pass |
test_object_create_with_aggregate (expressions.tests.BasicExpressionsTests) | Pass |
test_object_update (expressions.tests.BasicExpressionsTests) | Pass |
test_object_update_fk (expressions.tests.BasicExpressionsTests) | Pass |
test_object_update_unsaved_objects (expressions.tests.BasicExpressionsTests) | Pass |
test_order_by_exists (expressions.tests.BasicExpressionsTests) | Pass |
test_order_by_multiline_sql (expressions.tests.BasicExpressionsTests) | Pass |
test_order_of_operations (expressions.tests.BasicExpressionsTests) | Pass |
test_outerref (expressions.tests.BasicExpressionsTests) | Pass |
test_outerref_mixed_case_table_name (expressions.tests.BasicExpressionsTests) | Pass |
test_outerref_with_operator (expressions.tests.BasicExpressionsTests) | Pass |
test_parenthesis_priority (expressions.tests.BasicExpressionsTests) | Pass |
test_pickle_expression (expressions.tests.BasicExpressionsTests) | Pass |
test_subquery (expressions.tests.BasicExpressionsTests) | Pass |
test_subquery_eq (expressions.tests.BasicExpressionsTests) | Pass |
test_subquery_filter_by_aggregate (expressions.tests.BasicExpressionsTests) | Pass |
test_subquery_filter_by_lazy (expressions.tests.BasicExpressionsTests) | Pass |
test_subquery_group_by_outerref_in_filter (expressions.tests.BasicExpressionsTests) | Pass |
test_subquery_in_filter (expressions.tests.BasicExpressionsTests) | Pass |
test_subquery_references_joined_table_twice (expressions.tests.BasicExpressionsTests) | Pass |
test_ticket_11722_iexact_lookup (expressions.tests.BasicExpressionsTests) | Pass |
test_ticket_16731_startswith_lookup (expressions.tests.BasicExpressionsTests) | Pass |
test_ticket_18375_chained_filters (expressions.tests.BasicExpressionsTests) | Pass |
test_ticket_18375_join_reuse (expressions.tests.BasicExpressionsTests) | Pass |
test_ticket_18375_kwarg_ordering (expressions.tests.BasicExpressionsTests) | Pass |
test_ticket_18375_kwarg_ordering_2 (expressions.tests.BasicExpressionsTests) | Pass |
test_update (expressions.tests.BasicExpressionsTests) | Pass |
test_update_inherited_field_value (expressions.tests.BasicExpressionsTests) | Pass |
test_update_with_fk (expressions.tests.BasicExpressionsTests) | Pass |
test_update_with_none (expressions.tests.BasicExpressionsTests) | Pass |
test_uuid_pk_subquery (expressions.tests.BasicExpressionsTests) | Pass |
Loading...
Ridges.AI© 2025 Ridges AI. Building the future of decentralized AI development.
