Forums

OperationalError at /daily_stats/ (2006, '')

When I try to run raw SQL in my application it gives the below error:

> Environment:
> 
> 
> Request Method: GET Request URL:
> http://smrashelsebpo.pythonanywhere.com/daily_stats/
> 
> Django Version: 3.1.7 Python Version: 3.8.0 Installed Applications:
> ['django.contrib.admin',  'django.contrib.auth', 
> 'django.contrib.contenttypes',  'django.contrib.sessions', 
> 'django.contrib.messages',  'django.contrib.staticfiles', 
> 'solostove',  'crispy_forms'] Installed Middleware:
> ['django.middleware.security.SecurityMiddleware', 
> 'django.contrib.sessions.middleware.SessionMiddleware', 
> 'django.middleware.common.CommonMiddleware', 
> 'django.middleware.csrf.CsrfViewMiddleware', 
> 'django.contrib.auth.middleware.AuthenticationMiddleware', 
> 'django.contrib.messages.middleware.MessageMiddleware', 
> 'django.middleware.clickjacking.XFrameOptionsMiddleware']
> 
> 
> 
> Traceback (most recent call last):   File
> "/home/smrashelsebpo/venv/lib/python3.8/site-packages/django/db/backends/utils.py",
> line 84, in _execute
>     return self.cursor.execute(sql, params)   File "/home/smrashelsebpo/venv/lib/python3.8/site-packages/django/db/backends/mysql/base.py",
> line 73, in execute
>     return self.cursor.execute(query, args)   File "/home/smrashelsebpo/venv/lib/python3.8/site-packages/MySQLdb/cursors.py",
> line 183, in execute
>     while self.nextset():   File "/home/smrashelsebpo/venv/lib/python3.8/site-packages/MySQLdb/cursors.py",
> line 137, in nextset
>     nr = db.next_result()
> 
> The above exception ((2006, '')) was the direct cause of the following
> exception:   File
> "/home/smrashelsebpo/venv/lib/python3.8/site-packages/django/core/handlers/exception.py",
> line 47, in inner
>     response = get_response(request)   File "/home/smrashelsebpo/venv/lib/python3.8/site-packages/django/core/handlers/base.py",
> line 181, in _get_response
>     response = wrapped_callback(request, *callback_args, **callback_kwargs)   File "/home/smrashelsebpo/venv/lib/python3.8/site-packages/django/contrib/auth/decorators.py",
> line 21, in _wrapped_view
>     return view_func(request, *args, **kwargs)   File "/home/smrashelsebpo/sebpo/solostove/decorators.py", line 22, in
> wrapper_func
>     return view_func(request, *args, **kwargs)   File "/home/smrashelsebpo/sebpo/solostove/views.py", line 194, in
> daily_stats
>     user_stats = spDailyStats(start_date, end_date)   File "/home/smrashelsebpo/sebpo/solostove/stored_procedures.py", line 9, in
> spDailyStats
>     **cursor.execute("""SELECT auth_user.username AS ReviewedBy, DATE(end_time) AS ReviewDate, COUNT(solostove_task.id) AS Completed,
> SUM(TIME_TO_SEC(TIMEDIFF(end_time,start_time))) AS LoginHour,
> auth_user.id AS UserId FROM solostove_task LEFT JOIN auth_user ON
> solostove_task.added_by_id=auth_user.id WHERE end_time>= %s AND
> end_time< %s GROUP BY added_by_id, DATE(end_time);""", (start_date,
> end_date,))**   File
> "/home/smrashelsebpo/venv/lib/python3.8/site-packages/django/db/backends/utils.py",
> line 98, in execute
>     return super().execute(sql, params)   File "/home/smrashelsebpo/venv/lib/python3.8/site-packages/django/db/backends/utils.py",
> line 66, in execute
>     return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)   File
> "/home/smrashelsebpo/venv/lib/python3.8/site-packages/django/db/backends/utils.py",
> line 75, in _execute_with_wrappers
>     return executor(sql, params, many, context)   File "/home/smrashelsebpo/venv/lib/python3.8/site-packages/django/db/backends/utils.py",
> line 84, in _execute
>     return self.cursor.execute(sql, params)   File "/home/smrashelsebpo/venv/lib/python3.8/site-packages/django/db/utils.py",
> line 90, in __exit__
>     raise dj_exc_value.with_traceback(traceback) from exc_value   File "/home/smrashelsebpo/venv/lib/python3.8/site-packages/django/db/backends/utils.py",
> line 84, in _execute
>     return self.cursor.execute(sql, params)   File "/home/smrashelsebpo/venv/lib/python3.8/site-packages/django/db/backends/mysql/base.py",
> line 73, in execute
>     return self.cursor.execute(query, args)   File "/home/smrashelsebpo/venv/lib/python3.8/site-packages/MySQLdb/cursors.py",
> line 183, in execute
>     while self.nextset():   File "/home/smrashelsebpo/venv/lib/python3.8/site-packages/MySQLdb/cursors.py",
> line 137, in nextset
>     nr = db.next_result()
> 
> Exception Type: OperationalError at /daily_stats/ Exception Value:
> (2006, '')

The same code worked in my local machine. How can I solve this?

[edited by admin: formatting]

The 2006 error is "MySQL server has gone away". See this. Usually this is more easily done/managed automatically by Django, but perhaps if you are writing your own raw queries you may need to figure out how to manage it yourself.