#30224: Mysql Datetime value is string instead of datetime object -------------------------------+-------------------------------------- Reporter: upMKuhn | Owner: nobody Type: Uncategorized | Status: new Component: Uncategorized | Version: 2.2 Severity: Normal | Resolution: Keywords: Mysql | Triage Stage: Unreviewed Has patch: 0 | Needs documentation: 0 Needs tests: 0 | Patch needs improvement: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------+-------------------------------------- Description changed by upMKuhn:
Old description: > **The Problem** > Datetimes are sometimes returned as objects or strings. It depends on the > value. E.g. '2019-03-01 00:00:00.000000' is returned as a string, > while`2018-12-12 22:02:53.134000` is provided as a datetime object in the > sql/compiler.py. > > **My Table** > > > > CREATE TABLE `myTable` ( > > `id` char(32) COLLATE utf8mb4_unicode_ci NOT NULL, > > `created_on` datetime(6) NOT NULL, > > `modified_on` datetime(6) NOT NULL, > > `first_spot_broadcast` datetime(6) DEFAULT NULL, > > `expiry_date` datetime(6) DEFAULT NULL > > ) > > DB driver: django.db.backends.mysql > Django version: 2.0.4 tried 2.2 as well > DRF 3.8.2 > mysqlclient = "==1.4.2" > > This issue only occurred for me when I execute this query. > > {{{ > .annotate( > minExpiryDate=Min('buyout__expiry_date', > filter=Q(buyout__is_archived=False, buyout__is_deleted=False))) \ > .filter(Q(minExpiryDate__gte=(now + expiryInterval)) > & Q(minExpiryDate__lte=(now + expiryInterval + > timedelta(days=1)))) > }}} > > I tried upgrading to 2.2 and the issue persisted. > > > **My stack trace is: > ** > {{{ > Traceback (most recent call last): > File "/usr/share/pycharm/helpers/pydev/pydevd.py", line 1741, in > <module> > main() > File "/usr/share/pycharm/helpers/pydev/pydevd.py", line 1735, in main > globals = debugger.run(setup['file'], None, None, is_module) > File "/usr/share/pycharm/helpers/pydev/pydevd.py", line 1135, in run > pydev_imports.execfile(file, globals, locals) # execute the script > File "/usr/share/pycharm/helpers/pydev/_pydev_imps/_pydev_execfile.py", > line 18, in execfile > exec(compile(contents+"\n", file, 'exec'), glob, loc) > File "/server/manage.py", line 15, in <module> > execute_from_command_line(sys.argv) > File "site-packages/django/core/management/__init__.py", line 381, in > execute_from_command_line > utility.execute() > File "site-packages/django/core/management/__init__.py", line 375, in > execute > self.fetch_command(subcommand).run_from_argv(self.argv) > File "site-packages/django/core/management/base.py", line 316, in > run_from_argv > self.execute(*args, **cmd_options) > File "site-packages/django/core/management/base.py", line 353, in > execute > output = self.handle(*args, **options) > File > "/server/buyout/management/commands/send_expiry_reminder_email.py", line > 8, in handle > send_expiry_reminder_summary() > File "site-packages/celery/local.py", line 191, in __call__ > return self._get_current_object()(*a, **kw) > File "site-packages/celery/app/task.py", line 375, in __call__ > return self.run(*args, **kwargs) > File "/server/buyout/tasks/expiry_reminder.py", line 46, in > send_expiry_reminder_summary > if len(projects_expiring) > 0: > File "site-packages/django/db/models/query.py", line 250, in __len__ > self._fetch_all() > File "site-packages/django/db/models/query.py", line 1186, in > _fetch_all > self._result_cache = list(self._iterable_class(self)) > File "site-packages/django/db/models/query.py", line 63, in __iter__ > for row in compiler.results_iter(results): > File "site-packages/django/db/models/sql/compiler.py", line 1009, in > apply_converters > value = converter(value, expression, connection) > File "site-packages/django/db/backends/mysql/operations.py", line 252, > in convert_datetimefield_value > value = timezone.make_aware(value, self.connection.timezone) > File "site-packages/django/utils/timezone.py", line 264, in make_aware > return timezone.localize(value, is_dst=is_dst) > File "site-packages/pytz/__init__.py", line 222, in localize > if dt.tzinfo is not None: > AttributeError: 'str' object has no attribute 'tzinfo' > Exception ignored in: <generator object cursor_iter at 0x7f7ab3f88390> > Traceback (most recent call last): > File "site-packages/django/db/models/sql/compiler.py", line 1469, in > cursor_iter > cursor.close() > File "site-packages/MySQLdb/cursors.py", line 86, in close > while self.nextset(): > File "site-packages/MySQLdb/cursors.py", line 136, in nextset > nr = db.next_result() > MySQLdb._exceptions.OperationalError: (2006, '') > }}} New description: **The Problem** Datetimes are sometimes returned as objects or strings. It depends on the value. E.g. '2019-03-01 00:00:00.000000' is returned as a string, while`2018-12-12 22:02:53.134000` is provided as a datetime object in the sql/compiler.py. My Table > CREATE TABLE `myTable` ( > `id` char(32) COLLATE utf8mb4_unicode_ci NOT NULL, > `created_on` datetime(6) NOT NULL, > `modified_on` datetime(6) NOT NULL, > `first_spot_broadcast` datetime(6) DEFAULT NULL, > `expiry_date` datetime(6) DEFAULT NULL > ) DB driver: django.db.backends.mysql Django version: 2.0.4 tried 2.2 as well DRF 3.8.2 mysqlclient = "==1.4.2" This issue only occurred for me when I execute this query. I tried upgrading to 2.2 and the issue persisted. {{{ .annotate( minExpiryDate=Min('buyout__expiry_date', filter=Q(buyout__is_archived=False, buyout__is_deleted=False))) \ .filter(Q(minExpiryDate__gte=(now + expiryInterval)) & Q(minExpiryDate__lte=(now + expiryInterval + timedelta(days=1)))) }}} **My stack trace is: ** {{{ Traceback (most recent call last): File "/usr/share/pycharm/helpers/pydev/pydevd.py", line 1741, in <module> main() File "/usr/share/pycharm/helpers/pydev/pydevd.py", line 1735, in main globals = debugger.run(setup['file'], None, None, is_module) File "/usr/share/pycharm/helpers/pydev/pydevd.py", line 1135, in run pydev_imports.execfile(file, globals, locals) # execute the script File "/usr/share/pycharm/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/server/manage.py", line 15, in <module> execute_from_command_line(sys.argv) File "site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line utility.execute() File "site-packages/django/core/management/__init__.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "site-packages/django/core/management/base.py", line 316, in run_from_argv self.execute(*args, **cmd_options) File "site-packages/django/core/management/base.py", line 353, in execute output = self.handle(*args, **options) File "/server/buyout/management/commands/send_expiry_reminder_email.py", line 8, in handle send_expiry_reminder_summary() File "site-packages/celery/local.py", line 191, in __call__ return self._get_current_object()(*a, **kw) File "site-packages/celery/app/task.py", line 375, in __call__ return self.run(*args, **kwargs) File "/server/buyout/tasks/expiry_reminder.py", line 46, in send_expiry_reminder_summary if len(projects_expiring) > 0: File "site-packages/django/db/models/query.py", line 250, in __len__ self._fetch_all() File "site-packages/django/db/models/query.py", line 1186, in _fetch_all self._result_cache = list(self._iterable_class(self)) File "site-packages/django/db/models/query.py", line 63, in __iter__ for row in compiler.results_iter(results): File "site-packages/django/db/models/sql/compiler.py", line 1009, in apply_converters value = converter(value, expression, connection) File "site-packages/django/db/backends/mysql/operations.py", line 252, in convert_datetimefield_value value = timezone.make_aware(value, self.connection.timezone) File "site-packages/django/utils/timezone.py", line 264, in make_aware return timezone.localize(value, is_dst=is_dst) File "site-packages/pytz/__init__.py", line 222, in localize if dt.tzinfo is not None: AttributeError: 'str' object has no attribute 'tzinfo' Exception ignored in: <generator object cursor_iter at 0x7f7ab3f88390> Traceback (most recent call last): File "site-packages/django/db/models/sql/compiler.py", line 1469, in cursor_iter cursor.close() File "site-packages/MySQLdb/cursors.py", line 86, in close while self.nextset(): File "site-packages/MySQLdb/cursors.py", line 136, in nextset nr = db.next_result() MySQLdb._exceptions.OperationalError: (2006, '') }}} -- -- Ticket URL: <https://code.djangoproject.com/ticket/30224#comment:1> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/065.273d120ce93c8e883b73c39d7c43efd9%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.