Skip to content

Stale read when creating a TranscriptionEntity

Sentry Issue: ARKINDEX-BACKEND-1C8

KeyError: 'element'
  File "django/db/models/fields/related_descriptors.py", line 187, in __get__
    rel_obj = self.field.get_cached_value(instance)
  File "django/db/models/fields/mixins.py", line 15, in get_cached_value
    return instance._state.fields_cache[cache_name]

Element.DoesNotExist: Element matching query does not exist.
(11 additional frame(s) were not displayed)
...
  File "arkindex/documents/api/entities.py", line 238, in perform_create
  File "arkindex/documents/models.py", line 759, in clean
  File "django/db/models/fields/related_descriptors.py", line 205, in __get__
    rel_obj = self.get_object(instance)
  File "django/db/models/fields/related_descriptors.py", line 168, in get_object
    return qs.get(self.field.get_reverse_related_filter(instance))
  File "django/db/models/query.py", line 496, in get
    raise self.model.DoesNotExist(

TranscriptionEntity.clean tries to access the transcription's element to get its corpus, as well as the corpus of the entity. With enough replication lag, the element or the corpora might not be found. Two queries could be removed by comparing against the corpus_id instead of the corpus, and one query be removed by using a select_related within the API endpoint. Then we only need to add .using('default') on a single query.