Skip to content

Commit 27b399d

Browse files
smithdc1felixxm
authored andcommitted
Fixed #34547 -- Deprecated DatabaseOperations.field_cast_sql().
1 parent 500e010 commit 27b399d

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed

django/db/backends/base/operations.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import datetime
22
import decimal
33
import json
4+
import warnings
45
from importlib import import_module
56

67
import sqlparse
@@ -10,6 +11,7 @@
1011
from django.db.backends import utils
1112
from django.db.models.expressions import Col
1213
from django.utils import timezone
14+
from django.utils.deprecation import RemovedInDjango60Warning
1315
from django.utils.encoding import force_str
1416

1517

@@ -220,6 +222,13 @@ def field_cast_sql(self, db_type, internal_type):
220222
it in a WHERE statement. The resulting string should contain a '%s'
221223
placeholder for the column being searched against.
222224
"""
225+
warnings.warn(
226+
(
227+
"DatabaseOperations.field_cast_sql() is deprecated use "
228+
"DatabaseOperations.lookup_cast() instead."
229+
),
230+
RemovedInDjango60Warning,
231+
)
223232
return "%s"
224233

225234
def force_no_ordering(self):

django/db/models/lookups.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import itertools
22
import math
3+
import warnings
34

45
from django.core.exceptions import EmptyResultSet, FullResultSet
6+
from django.db.backends.base.operations import BaseDatabaseOperations
57
from django.db.models.expressions import Case, Expression, Func, Value, When
68
from django.db.models.fields import (
79
BooleanField,
@@ -13,6 +15,7 @@
1315
)
1416
from django.db.models.query_utils import RegisterLookupMixin
1517
from django.utils.datastructures import OrderedSet
18+
from django.utils.deprecation import RemovedInDjango60Warning
1619
from django.utils.functional import cached_property
1720
from django.utils.hashable import make_hashable
1821

@@ -217,8 +220,22 @@ class BuiltinLookup(Lookup):
217220
def process_lhs(self, compiler, connection, lhs=None):
218221
lhs_sql, params = super().process_lhs(compiler, connection, lhs)
219222
field_internal_type = self.lhs.output_field.get_internal_type()
220-
db_type = self.lhs.output_field.db_type(connection=connection)
221-
lhs_sql = connection.ops.field_cast_sql(db_type, field_internal_type) % lhs_sql
223+
if (
224+
hasattr(connection.ops.__class__, "field_cast_sql")
225+
and connection.ops.__class__.field_cast_sql
226+
is not BaseDatabaseOperations.field_cast_sql
227+
):
228+
warnings.warn(
229+
(
230+
"The usage of DatabaseOperations.field_cast_sql() is deprecated. "
231+
"Implement DatabaseOperations.lookup_cast() instead."
232+
),
233+
RemovedInDjango60Warning,
234+
)
235+
db_type = self.lhs.output_field.db_type(connection=connection)
236+
lhs_sql = (
237+
connection.ops.field_cast_sql(db_type, field_internal_type) % lhs_sql
238+
)
222239
lhs_sql = (
223240
connection.ops.lookup_cast(self.lookup_name, field_internal_type) % lhs_sql
224241
)

docs/internals/deprecation.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ details on these changes.
4040

4141
* Support for ``cx_Oracle`` will be removed.
4242

43+
* ``BaseDatabaseOperations.field_cast_sql()`` will be removed.
44+
4345
.. _deprecation-removed-in-5.1:
4446

4547
5.1

docs/releases/5.0.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,11 @@ Miscellaneous
663663
* Support for ``cx_Oracle`` is deprecated in favor of `oracledb`_ 1.3.2+ Python
664664
driver.
665665

666+
* ``DatabaseOperations.field_cast_sql()`` is deprecated in favor of
667+
``DatabaseOperations.lookup_cast()``. Starting with Django 6.0,
668+
``BuiltinLookup.process_lhs()`` will no longer call ``field_cast_sql()``.
669+
Third-party database backends should implement ``lookup_cast()`` instead.
670+
666671
.. _`oracledb`: https://oracle.github.io/python-oracledb/
667672

668673
Features removed in 5.0

tests/backends/base/test_operations.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import decimal
2+
from unittest import mock
23

34
from django.core.management.color import no_style
45
from django.db import NotSupportedError, connection, transaction
56
from django.db.backends.base.operations import BaseDatabaseOperations
67
from django.db.models import DurationField, Value
78
from django.db.models.expressions import Col
9+
from django.db.models.lookups import Exact
810
from django.test import (
911
SimpleTestCase,
1012
TestCase,
@@ -13,6 +15,7 @@
1315
skipIfDBFeature,
1416
)
1517
from django.utils import timezone
18+
from django.utils.deprecation import RemovedInDjango60Warning
1619

1720
from ..models import Author, Book
1821

@@ -235,3 +238,24 @@ def test_execute_sql_flush_statements(self):
235238
self.assertEqual(author.pk, 1)
236239
book = Book.objects.create(author=author)
237240
self.assertEqual(book.pk, 1)
241+
242+
243+
class DeprecationTests(TestCase):
244+
def test_field_cast_sql_warning(self):
245+
base_ops = BaseDatabaseOperations(connection=connection)
246+
msg = (
247+
"DatabaseOperations.field_cast_sql() is deprecated use "
248+
"DatabaseOperations.lookup_cast() instead."
249+
)
250+
with self.assertRaisesMessage(RemovedInDjango60Warning, msg):
251+
base_ops.field_cast_sql("integer", "IntegerField")
252+
253+
def test_field_cast_sql_usage_warning(self):
254+
compiler = Author.objects.all().query.get_compiler(connection.alias)
255+
msg = (
256+
"The usage of DatabaseOperations.field_cast_sql() is deprecated. Implement "
257+
"DatabaseOperations.lookup_cast() instead."
258+
)
259+
with mock.patch.object(connection.ops.__class__, "field_cast_sql"):
260+
with self.assertRaisesMessage(RemovedInDjango60Warning, msg):
261+
Exact("name", "book__author__name").as_sql(compiler, connection)

0 commit comments

Comments
 (0)