Duplicate element paths when adding an element that has children to more than two parents
The unique constraint added with !1383 (merged) caught a bug!
Sentry Issue: ARKINDEX-BACKEND-PQ
UniqueViolation: duplicate key value violates unique constraint "unique_element_paths"
DETAIL: Key (element_id, path)=(e2cc3971-07e4-4557-a726-96091c4bbc08, {3b7b3f34-49e1-4ce2-ba82-b0bd108805bc,7ea442e3-b31f-4b6a-b5b0-84f4d8d92568}) 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 "unique_element_paths"
DETAIL: Key (element_id, path)=(e2cc3971-07e4-4557-a726-96091c4bbc08, {3b7b3f34-49e1-4ce2-ba82-b0bd108805bc,7ea442e3-b31f-4b6a-b5b0-84f4d8d92568}) already exists.
(19 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)
To reproduce:
# Create three folders.
folder1 = cli.request('CreateElement', body={'corpus': corpus_id, 'type': 'folder', 'name': 'ElementPath test 1'})
folder2 = cli.request('CreateElement', body={'corpus': corpus_id, 'type': 'folder', 'name': 'ElementPath test 2'})
folder3 = cli.request('CreateElement', body={'corpus': corpus_id, 'type': 'folder', 'name': 'ElementPath test 3'})
# Create an element in one of the folders, and give it any child element.
page = cli.request('CreateElement', body={'corpus': corpus_id, 'type': 'page', 'name': 'ElementPath test page', 'parent': folder['id']})
cli.request('CreateElement', body={'corpus': corpus_id, 'type': 'page', 'name': 'ElementPath test thing', 'parent': page['id']})
# Add the element to the second folder; this will work...
cli.request('CreateElementParent', parent=folder2['id'], child=page['id'])
# ...but on the third folder, it catches fire.
cli.request('CreateElementParent', parent=folder3['id'], child=page['id'])
Edited by Erwan Rouchet