Duplicate UserStory ref after jira import

After Jira import and making some stories (I think mostly made from the sprint view) some of them don’t show up when clicking on them/return a 500

looking at the logs I get

aiga-docker-taiga-back-1 | ERROR:2023-03-22 22:11:42,085: Internal Server Error: /api/v1/userstories/by_ref
taiga-docker-taiga-back-1 | Traceback (most recent call last):
taiga-docker-taiga-back-1 | File “/opt/venv/lib/python3.7/site-packages/django/core/handlers/exception.py”, line 34, in inner
taiga-docker-taiga-back-1 | response = get_response(request)
taiga-docker-taiga-back-1 | File “/opt/venv/lib/python3.7/site-packages/django/core/handlers/base.py”, line 115, in _get_response
taiga-docker-taiga-back-1 | response = self.process_exception_by_middleware(e, request)
taiga-docker-taiga-back-1 | File “/opt/venv/lib/python3.7/site-packages/django/core/handlers/base.py”, line 113, in _get_response
taiga-docker-taiga-back-1 | response = wrapped_callback(request, *callback_args, **callback_kwargs)
taiga-docker-taiga-back-1 | File “/taiga-back/taiga/base/api/viewsets.py”, line 95, in view
taiga-docker-taiga-back-1 | return self.dispatch(request, *args, **kwargs)
taiga-docker-taiga-back-1 | File “/opt/venv/lib/python3.7/site-packages/django/views/decorators/csrf.py”, line 54, in wrapped_view
taiga-docker-taiga-back-1 | return view_func(*args, **kwargs)
taiga-docker-taiga-back-1 | File “/taiga-back/taiga/base/api/views.py”, line 449, in dispatch
taiga-docker-taiga-back-1 | response = self.handle_exception(exc)
taiga-docker-taiga-back-1 | File “/taiga-back/taiga/base/api/views.py”, line 447, in dispatch
taiga-docker-taiga-back-1 | response = handler(request, *args, **kwargs)
taiga-docker-taiga-back-1 | File “/taiga-back/taiga/projects/mixins/by_ref.py”, line 37, in by_ref
taiga-docker-taiga-back-1 | return self.retrieve(request, **retrieve_kwargs)
taiga-docker-taiga-back-1 | File “/taiga-back/taiga/base/api/mixins.py”, line 140, in retrieve
taiga-docker-taiga-back-1 | self.object = get_object_or_404(self.get_queryset(), **kwargs)
taiga-docker-taiga-back-1 | File “/taiga-back/taiga/base/api/utils/init.py”, line 46, in get_object_or_404
taiga-docker-taiga-back-1 | return _get_object_or_404(queryset, *filter_args, **filter_kwargs)
taiga-docker-taiga-back-1 | File “/opt/venv/lib/python3.7/site-packages/django/shortcuts.py”, line 93, in get_object_or_404
taiga-docker-taiga-back-1 | return queryset.get(*args, **kwargs)
taiga-docker-taiga-back-1 | File “/opt/venv/lib/python3.7/site-packages/django/db/models/query.py”, line 412, in get
taiga-docker-taiga-back-1 | (self.model._meta.object_name, num)
taiga-docker-taiga-back-1 | taiga.projects.userstories.models.UserStory.MultipleObjectsReturned: get() returned more than one UserStory – it returned 2!

Looking at the database I get several dupes. How do I fix it? can I delete some of the dupes from the database? or change the ref number?
Is there a bug filed for this? How do I prevent it in the future since this seems like a serious bug.

@david.barragan any clues? I’ve had this open for over a week and haven’t had any response.

Would it be safe to renumber the refs do they refer to anything or are they only used for sorting?
I still worry a bit that it will show up again, is there a way to prevent that/how are refs incremented?

I opened a bug about it (Taiga) since it seems like a bug either with the import code or the db model or both

ref = models.BigIntegerField(db_index=True, null=True, blank=True, default=None,

Hello RT-AP,

It’s the first time I see this error. The references are generated from a PostgreSQL sequence associated with the project and it’s very rare that two elements with the same id can be generated.

Try to identify the number of elements with repeated refs to check if there are many or few cases.

You can delete one of them or modify the ref number, although you will have to increase the value of the sequence in PostgreSQL so that the new number is not used again.

TIP: You can use the django shell to obtains and recalculate the sequences of a project

make_unique_reference_id() returns the next valid value that you can use and recalc_reference_counter() is useful to sync the project sequence.

I’m sorry I can’t be of more help, but it is very complicated for me to reproduce the environment to test the import from an on-premise instance of Jira.

after renumbering one entry (it seemed to work but maybe I should renumber it again with make_unique_reference_id) I get 5 twin refs (so 10 with a single duplicate and before probably 12 entries)

931-935 out of 886 rows in the project (refs number 1-1129 with holes). I did try to import this project multiple times but I dont think thats the issue. duplicates seem to be new stories. I also didn’t mess with the db before.

make_unique_reference_id gave me invalid (duplicate) refs then I called recalc_reference_counter and then make_unique_reference_id gave me valid ones