Skip to content

ValueError: MySQL backend does not support timezone-aware datetimes when USE_TZ is False. #951

@dxygit1

Description

@dxygit1

version

celery                        5.5.1
django-celery-beat            2.8.1
django_celery_results         2.6.0

django config

USE_TZ = False  
TIME_ZONE = 'Asia/Shanghai'
CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
CELERY_WORKER_CONCURRENCY = 50
CELERY_WORKER_PREFETCH_MULTIPLIER = 20
CELERY_WORKER_MAX_TASKS_PER_CHILD = 100
CELERY_WORKER_DISABLE_RATE_LIMITS = True
CELERY_ENABLE_UTC = False
CELERY_DJANGO_CELERY_BEAT_TZ_AWARE = False
CELERY_TASK_TIME_LIMIT = 60 * 60
CELERY_BROKER_CONNECTION_RETRY_ON_STARTUP = True

celery

from celery import Celery
app = Celery('desk_api')

app.config_from_object('django.conf:settings', namespace='CELERY')

task

             id: 106
           name: celery.backend_cleanup
           task: celery.backend_cleanup
           args: []
         kwargs: {}
          queue: NULL
       exchange: NULL
    routing_key: NULL
        expires: NULL
        enabled: 1
    last_run_at: NULL
total_run_count: 0
   date_changed: 2025-09-26 11:22:35.985126
    description: 
     crontab_id: 1
    interval_id: NULL
       solar_id: NULL
        one_off: 0
     start_time: NULL
       priority: NULL
        headers: {}
     clocked_id: NULL
 expire_seconds: 43200


mysql> select * from django_celery_beat_crontabschedule;
+----+--------+------+-------------+--------------+---------------+---------------+
| id | minute | hour | day_of_week | day_of_month | month_of_year | timezone      |
+----+--------+------+-------------+--------------+---------------+---------------+
|  1 | 0      | 4    | *           | *            | *             | Asia/Shanghai |

error

[2025-09-26 11:22:35,916: INFO/MainProcess] beat: Starting...
[2025-09-26 11:27:36,266: INFO/MainProcess] Scheduler: Sending due task celery.backend_cleanup (celery.backend_cleanup)
[2025-09-26 11:28:41,343: CRITICAL/MainProcess] beat raised exception <class 'ValueError'>: ValueError('MySQL backend does not support timezone-aware date
times when USE_TZ is False.')
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/celery/apps/beat.py", line 113, in start_scheduler
    service.start()
  File "/usr/local/lib/python3.12/site-packages/celery/beat.py", line 650, in start
    self.scheduler._do_sync()
  File "/usr/local/lib/python3.12/site-packages/celery/beat.py", line 429, in _do_sync
    self.sync()
  File "/usr/local/lib/python3.12/site-packages/django_celery_beat/schedulers.py", line 453, in sync
    self._schedule[name].save()
  File "/usr/local/lib/python3.12/site-packages/django_celery_beat/schedulers.py", line 179, in save
    obj.save()
  File "/usr/local/lib/python3.12/site-packages/django_celery_beat/models.py", line 628, in save
    super().save(*args, **kwargs)
  File "/usr/local/lib/python3.12/site-packages/django/db/models/base.py", line 814, in save
    self.save_base(
  File "/usr/local/lib/python3.12/site-packages/django/db/models/base.py", line 877, in save_base
    updated = self._save_table(
              ^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/models/base.py", line 990, in _save_table
    updated = self._do_update(
              ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/models/base.py", line 1054, in _do_update
    return filtered._update(values) > 0
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/models/query.py", line 1231, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/models/sql/compiler.py", line 1984, in execute_sql
    cursor = super().execute_sql(result_type)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/models/sql/compiler.py", line 1549, in execute_sql
    sql, params = self.as_sql()
                  ^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/backends/mysql/compiler.py", line 57, in as_sql
    update_query, update_params = super().as_sql()
                                  ^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/models/sql/compiler.py", line 1947, in as_sql
    val = field.get_db_prep_save(val, connection=self.connection)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/models/fields/__init__.py", line 954, in get_db_prep_save
    return self.get_db_prep_value(value, connection=connection, prepared=False)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/models/fields/__init__.py", line 1608, in get_db_prep_value
    return connection.ops.adapt_datetimefield_value(value)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/db/backends/mysql/operations.py", line 268, in adapt_datetimefield_value
    raise ValueError(
ValueError: MySQL backend does not support timezone-aware datetimes when USE_TZ is False.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions