Skip to content

Commit

Permalink
[3.2.x] Fixed #32392 -- Fixed ExclusionConstraint crash with Cast() i…
Browse files Browse the repository at this point in the history
…n expressions.

Backport of fdfbc66 from master
  • Loading branch information
tkoschnick authored and felixxm committed Jan 29, 2021
1 parent f4d51e1 commit 18cac6b
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 23 deletions.
6 changes: 0 additions & 6 deletions django/db/models/functions/comparison.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ def as_mysql(self, compiler, connection, **extra_context):
template = "JSON_EXTRACT(%(expressions)s, '$')"
return self.as_sql(compiler, connection, template=template, **extra_context)

def as_postgresql(self, compiler, connection, **extra_context):
# CAST would be valid too, but the :: shortcut syntax is more readable.
# 'expressions' is wrapped in parentheses in case it's a complex
# expression.
return self.as_sql(compiler, connection, template='(%(expressions)s)::%(db_type)s', **extra_context)

def as_oracle(self, compiler, connection, **extra_context):
if self.output_field.get_internal_type() == 'JSONField':
# Oracle doesn't support explicit cast to JSON.
Expand Down
15 changes: 1 addition & 14 deletions tests/db_functions/comparison/test_cast.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import datetime
import decimal
import unittest

from django.db import connection, models
from django.db.models.functions import Cast
from django.test import (
TestCase, ignore_warnings, override_settings, skipUnlessDBFeature,
)
from django.test import TestCase, ignore_warnings, skipUnlessDBFeature

from ..models import Author, DTModel, Fan, FloatModel

Expand Down Expand Up @@ -128,15 +125,5 @@ def test_cast_from_python(self):
self.assertIsInstance(cast_float, float)
self.assertEqual(cast_float, 0.125)

@unittest.skipUnless(connection.vendor == 'postgresql', 'PostgreSQL test')
@override_settings(DEBUG=True)
def test_expression_wrapped_with_parentheses_on_postgresql(self):
"""
The SQL for the Cast expression is wrapped with parentheses in case
it's a complex expression.
"""
list(Author.objects.annotate(cast_float=Cast(models.Avg('age'), models.FloatField())))
self.assertIn('(AVG("db_functions_author"."age"))::double precision', connection.queries[-1]['sql'])

def test_cast_to_text_field(self):
self.assertEqual(Author.objects.values_list(Cast('age', models.TextField()), flat=True).get(), '1')
15 changes: 13 additions & 2 deletions tests/postgres_tests/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
IntegrityError, NotSupportedError, connection, transaction,
)
from django.db.models import (
CheckConstraint, Deferrable, F, Func, Q, UniqueConstraint,
CheckConstraint, Deferrable, F, Func, IntegerField, Q, UniqueConstraint,
)
from django.db.models.fields.json import KeyTextTransform
from django.db.models.functions import Left
from django.db.models.functions import Cast, Left
from django.test import skipUnlessDBFeature
from django.utils import timezone

Expand Down Expand Up @@ -783,3 +783,14 @@ def test_range_adjacent_opclasses_include(self):
with connection.schema_editor() as editor:
editor.add_constraint(RangesModel, constraint)
self.assertIn(constraint_name, self.get_constraints(RangesModel._meta.db_table))

def test_range_equal_cast(self):
constraint_name = 'exclusion_equal_room_cast'
self.assertNotIn(constraint_name, self.get_constraints(Room._meta.db_table))
constraint = ExclusionConstraint(
name=constraint_name,
expressions=[(Cast('number', IntegerField()), RangeOperators.EQUAL)],
)
with connection.schema_editor() as editor:
editor.add_constraint(Room, constraint)
self.assertIn(constraint_name, self.get_constraints(Room._meta.db_table))
2 changes: 1 addition & 1 deletion tests/postgres_tests/test_indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ def test_cast_search_vector_gin_index(self):
self.assertIn(index_name, constraints)
self.assertIn(constraints[index_name]['type'], GinIndex.suffix)
self.assertIs(sql.references_column(table, 'field'), True)
self.assertIn('::tsvector', str(sql))
self.assertIn(' AS tsvector', str(sql))
with connection.schema_editor() as editor:
editor.remove_index(TextFieldModel, index)
self.assertNotIn(index_name, self.get_constraints(table))
Expand Down

0 comments on commit 18cac6b

Please sign in to comment.