Skip to content

Stale read when computing a new metadata's index

Sentry Issue: ARKINDEX-BACKEND-4X

UniqueViolation: duplicate key value violates unique constraint "documents_metadata_element_id_name_index_b9ba6f8d_uniq"
DETAIL:  Key (element_id, name, index)=(9721cb37-9cbc-4f3c-887e-9ef7ad135388, Collection, 0) already exists.

  File "django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)

IntegrityError: duplicate key value violates unique constraint "documents_metadata_element_id_name_index_b9ba6f8d_uniq"
DETAIL:  Key (element_id, name, index)=(9721cb37-9cbc-4f3c-887e-9ef7ad135388, Collection, 0) already exists.

(24 additional frame(s) were not displayed)
...
  File "django/db/backends/utils.py", line 66, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "django/db/backends/utils.py", line 75, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "django/db/utils.py", line 90, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)

This occurs when creating a metadata with the same name as one that was not replicated yet; the backend could not see the first metadata and assumed it could just create it with an index of 0. It also makes it possible for metadata with duplicate values to appear, although we block that in the API.