1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Refs #28329 -- Fixed Cast() with ForeignKey to integer fields on MySQL.

This commit is contained in:
David Wobrock 2023-03-06 21:17:58 +01:00 committed by Mariusz Felisiak
parent 254ad2e345
commit de0c7744be
2 changed files with 12 additions and 0 deletions

View File

@ -1162,6 +1162,9 @@ class ForeignKey(ForeignObject):
def db_type(self, connection):
return self.target_field.rel_db_type(connection=connection)
def cast_db_type(self, connection):
return self.target_field.cast_db_type(connection=connection)
def db_parameters(self, connection):
target_db_parameters = self.target_field.db_parameters(connection)
return {

View File

@ -76,6 +76,15 @@ class CastTests(TestCase):
numbers = Author.objects.annotate(cast_int=Cast("alias", field_class()))
self.assertEqual(numbers.get().cast_int, 1)
def test_cast_to_integer_foreign_key(self):
numbers = Author.objects.annotate(
cast_fk=Cast(
models.Value("0"),
models.ForeignKey(Author, on_delete=models.SET_NULL),
)
)
self.assertEqual(numbers.get().cast_fk, 0)
def test_cast_to_duration(self):
duration = datetime.timedelta(days=1, seconds=2, microseconds=3)
DTModel.objects.create(duration=duration)