Fixed #31987 -- Fixed Cast() with DurationField on MySQL.

This commit is contained in:
Petr Čermák 2020-09-08 16:21:41 +02:00 committed by Mariusz Felisiak
parent 5ea1621c72
commit fc1446073e
2 changed files with 11 additions and 0 deletions

View File

@ -29,6 +29,7 @@ class DatabaseOperations(BaseDatabaseOperations):
'PositiveBigIntegerField': 'unsigned integer',
'PositiveIntegerField': 'unsigned integer',
'PositiveSmallIntegerField': 'unsigned integer',
'DurationField': 'signed integer',
}
cast_char_field_without_max_length = 'char'
explain_prefix = 'EXPLAIN'

View File

@ -65,6 +65,16 @@ class CastTests(TestCase):
numbers = Author.objects.annotate(cast_int=Cast('alias', field_class()))
self.assertEqual(numbers.get().cast_int, 1)
def test_cast_to_duration(self):
duration = datetime.timedelta(days=1, seconds=2, microseconds=3)
DTModel.objects.create(duration=duration)
dtm = DTModel.objects.annotate(
cast_duration=Cast('duration', models.DurationField()),
cast_neg_duration=Cast(-duration, models.DurationField()),
).get()
self.assertEqual(dtm.cast_duration, duration)
self.assertEqual(dtm.cast_neg_duration, -duration)
def test_cast_from_db_datetime_to_date(self):
dt_value = datetime.datetime(2018, 9, 28, 12, 42, 10, 234567)
DTModel.objects.create(start_datetime=dt_value)