From fdfbc66331292def201c9344e3cd29fbcbcd076a Mon Sep 17 00:00:00 2001 From: Tilman Koschnick Date: Thu, 28 Jan 2021 17:50:54 +0100 Subject: [PATCH] Fixed #32392 -- Fixed ExclusionConstraint crash with Cast() in expressions. --- django/db/models/functions/comparison.py | 6 ------ tests/db_functions/comparison/test_cast.py | 15 +-------------- tests/postgres_tests/test_constraints.py | 15 +++++++++++++-- tests/postgres_tests/test_indexes.py | 2 +- 4 files changed, 15 insertions(+), 23 deletions(-) diff --git a/django/db/models/functions/comparison.py b/django/db/models/functions/comparison.py index 8a1c34430bf..2976985db88 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 18344fe6560..687cf77656e 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 8621d7a052c..1bf52d0cc0b 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 a212de2087c..3578fb68f39 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))