Django's validate_no_broken_transaction не работает для запроса, который отлично работает в консоли

Я использую django 2.2.25 с mysql 8.0.27. Время от времени в моих журналах появляется такая ошибка:

Traceback (most recent call last):
  File "/home/v/django/django_projects/v/tele/bot.py", line 139, in tmp
    res = f(bot, update, *args, **kwargs)
  File "/home/v/django/django_projects/v/tele/bot.py", line 401, in answer_action
    finalize_game(bot, update, prize, prefix=text)
  File "/usr/lib/python3.8/contextlib.py", line 75, in inner
    return func(*args, **kwds)
  File "<string>", line 2, in finalize_game
  File "/home/v/.local/lib/python3.8/site-packages/retry/api.py", line 73, in retry_decorator
    return __retry_internal(partial(f, *args, **kwargs), exceptions, tries, delay, max_delay, backoff, jitter,
  File "/home/v/.local/lib/python3.8/site-packages/retry/api.py", line 33, in __retry_internal
    return f()
  File "/home/v/django/django_projects/v/tele/bot.py", line 341, in finalize_game
    if len(users) == 0:
  File "/home/v/.local/lib/python3.8/site-packages/django/db/models/query.py", line 256, in __len__
    self._fetch_all()
  File "/home/v/.local/lib/python3.8/site-packages/django/db/models/query.py", line 1242, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/home/v/.local/lib/python3.8/site-packages/django/db/models/query.py", line 55, in __iter__
    results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
  File "/home/v/.local/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1142, in execute_sql
    cursor.execute(sql, params)
  File "/home/v/.local/lib/python3.8/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/v/.local/lib/python3.8/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/v/.local/lib/python3.8/site-packages/django/db/backends/utils.py", line 79, in _execute
    self.db.validate_no_broken_transaction()
  File "/home/v/.local/lib/python3.8/site-packages/django/db/backends/base/base.py", line 437, in validate_no_broken_transaction
    raise TransactionManagementError(
django.db.transaction.TransactionManagementError: An error occurred in the current transaction. You can't execute queries until the end of the 'atomic' block.

Мой код:

@atomic
@retry(tries=3, delay=1)
def finalize_game(bot, update, prize, prefix=None):
    state = game_states.get(update.message.chat.id, None)
    uid = update.message.from_user.id
    log_message('Check users for %s' % uid)
    users = TelegramUser.objects.filter(telegram_id=uid).all()
    update_user = update.message.from_user
    if len(users) == 0: # Failure 

Django запрос для TelegramUser.objects.filter(telegram_id=uid).all() должен быть довольно простым, когда я вызываю что-то вроде select * from game_telegramuser where telegram_id=<id that failed>; в консоли mysql, он работает нормально.

Может кто-нибудь подсказать, почему эта транзакция помечается как нарушенная при выполнении внутри кода django?

У меня была мысль, что возможно id слишком длинный, но изменил тип колонки на bigint и все равно ошибка есть, иногда для маленьких id.

Вернуться на верх