Skip to content

Commit 93c7b38

Browse files
Robert LuRobert Lu
authored andcommitted
fix datetime colume truncated error
error esg: DatabaseError: Data truncated for column <applied> at row 1
1 parent 24a5ee7 commit 93c7b38

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

lib/mysql/connector/django/operations.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,22 @@ def date_extract_sql(self, lookup_type, field_name):
4343
return "EXTRACT({0} FROM {1})".format(
4444
lookup_type.upper(), field_name)
4545

46+
def adapt_datetimefield_value(self, value):
47+
if value is None:
48+
return None
49+
50+
# MySQL doesn't support tz-aware datetimes
51+
if timezone.is_aware(value):
52+
if settings.USE_TZ:
53+
value = timezone.make_naive(value, self.connection.timezone)
54+
else:
55+
raise ValueError("MySQL backend does not support timezone-aware datetimes when USE_TZ is False.")
56+
57+
if not self.connection.features.supports_microsecond_precision:
58+
value = value.replace(microsecond=0)
59+
60+
return force_text(value)
61+
4662
def date_trunc_sql(self, lookup_type, field_name):
4763
"""Returns SQL simulating DATE_TRUNC
4864

0 commit comments

Comments
 (0)