Skip to content

Stale read when fetching the element's image in CreateElementTranscriptions

Sentry Issue: ARKINDEX-BACKEND-1KS

KeyError: 'image'
  File "django/db/models/fields/related_descriptors.py", line 189, 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]

Image.DoesNotExist: Image matching query does not exist.
(12 additional frame(s) were not displayed)
...
  File "rest_framework/serializers.py", line 422, in run_validation
    value = self.validate(value)
  File "arkindex/documents/serializers/ml.py", line 424, in validate
  File "django/db/models/fields/related_descriptors.py", line 207, in __get__
    rel_obj = self.get_object(instance)
  File "django/db/models/fields/related_descriptors.py", line 170, in get_object
    return qs.get(self.field.get_reverse_related_filter(instance))
  File "django/db/models/query.py", line 650, in get
    raise self.model.DoesNotExist(

The element itself is already retrieved using the default DB, and whether or not it has an image is checked with element.image_id to avoid fetching the image separately, but the image is fetched anyway by the serializer to verify that all polygons fit within the element's bounds. The image is fetched automatically by Django in a N+1 query when using element.image, which uses the replica and therefore can cause a stale read.