summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTilman Koschnick <til@subnetz.org>2021-01-28 17:50:54 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-01-29 11:05:00 +0100
commitfdfbc66331292def201c9344e3cd29fbcbcd076a (patch)
tree42e10a5a20c101ed7cb5eeb172090147356dfb2a
parent135c800fe6138d7818501a384c0ebbdc5442762c (diff)
Fixed #32392 -- Fixed ExclusionConstraint crash with Cast() in expressions.
-rw-r--r--django/db/models/functions/comparison.py6
-rw-r--r--tests/db_functions/comparison/test_cast.py15
-rw-r--r--tests/postgres_tests/test_constraints.py15
-rw-r--r--tests/postgres_tests/test_indexes.py2
4 files changed, 15 insertions, 23 deletions
diff --git a/django/db/models/functions/comparison.py b/django/db/models/functions/comparison.py
index 8a1c34430b..2976985db8 100644
--- a/django/db/models/functions/comparison.py
+++ b/django/db/models/functions/comparison.py
@@ -42,12 +42,6 @@ class Cast(Func):
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.
diff --git a/tests/db_functions/comparison/test_cast.py b/tests/db_functions/comparison/test_cast.py
index 18344fe656..687cf77656 100644
--- a/tests/db_functions/comparison/test_cast.py
+++ b/tests/db_functions/comparison/test_cast.py
@@ -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
@@ -128,15 +125,5 @@ class CastTests(TestCase):
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')
diff --git a/tests/postgres_tests/test_constraints.py b/tests/postgres_tests/test_constraints.py
index 8621d7a052..1bf52d0cc0 100644
--- a/tests/postgres_tests/test_constraints.py
+++ b/tests/postgres_tests/test_constraints.py
@@ -5,10 +5,10 @@ from django.db import (
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
@@ -783,3 +783,14 @@ class ExclusionConstraintTests(PostgreSQLTestCase):
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))
diff --git a/tests/postgres_tests/test_indexes.py b/tests/postgres_tests/test_indexes.py
index a212de2087..3578fb68f3 100644
--- a/tests/postgres_tests/test_indexes.py
+++ b/tests/postgres_tests/test_indexes.py
@@ -305,7 +305,7 @@ class SchemaTests(PostgreSQLTestCase):
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))