Laimonas Sutkus
Laimonas Sutkus

Reputation: 3627

Django ORM key error with lost MySql connection

This is the environment:

  1. AWS Aurora database compatible with MySql.

  2. Django 2.0.3 (Python 3.6)

  3. Pip-Mysql dependencies: django-mysql==2.2.2, mysqlclient==1.3.12.

  4. Master-Slave database configuration.

It seems that django or mysql engine always fails on certain queries resulting in this specific error:

Traceback (most recent call last): File "/home/ubuntu/ivs/vpython/lib/python3.6/site-packages/django/db/models/fields/related_descriptors.py", line 158, in get rel_obj = self.field.get_cached_value(instance) File "/home/ubuntu/ivs/vpython/lib/python3.6/site-packages/django/db/models/fields/mixins.py", line 13, in get_cached_value return instance._state.fields_cache[cache_name] KeyError: 'assigned_to'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "/home/ubuntu/ivs/vpython/lib/python3.6/site-packages/django/db/backends/utils.py", line 85, in _execute return self.cursor.execute(sql, params) File "/home/ubuntu/ivs/vpython/lib/python3.6/site-packages/django/db/backends/mysql/base.py", line 71, in execute return self.cursor.execute(query, args) File "/home/ubuntu/ivs/vpython/lib/python3.6/site-packages/MySQLdb/cursors.py", line 253, in execute self._warning_check() File "/home/ubuntu/ivs/vpython/lib/python3.6/site-packages/MySQLdb/cursors.py", line 148, in _warning_check warnings = db.show_warnings() File "/home/ubuntu/ivs/vpython/lib/python3.6/site-packages/MySQLdb/connections.py", line 381, in show_warnings self.query("SHOW WARNINGS") File "/home/ubuntu/ivs/vpython/lib/python3.6/site-packages/MySQLdb/connections.py", line 277, in query _mysql.connection.query(self, query) _mysql_exceptions.OperationalError: (2013, 'Lost connection to MySQL server during query')

Yes, one of my models have "assigend_to" field which is a foreign key. But why does it fail with a KeyError?

Did anyone have any similar KeyErrors and MySql lost connections as a result?

Upvotes: 1

Views: 983

Answers (2)

Laimonas Sutkus
Laimonas Sutkus

Reputation: 3627

Wow, what actually was happening is this:

  1. I was making queries with reverse-foreign keys.
  2. Objects returned with with reverse-foreign keys contained some other foreign keys.
  3. When I tried to access them e.g. 'assigned_to' i got this exception every time.

Upvotes: 0

cagrias
cagrias

Reputation: 1847

In official MySQL definition, there is a specific page about this error. As can be seen from here in detail, here are what you should try:

  1. The "during query" term in your error might be connected with a timeout caused by processing millions of rows. You can increase net_read_timeout to 60 seconds etc.
  2. This can happen when you are trying the initial connection but in your case this is impossible.

Upvotes: 1

Related Questions