# Duplicate UserStory ref after jira import

**URL:** <https://community.taiga.io/t/duplicate-userstory-ref-after-jira-import/932>\
**Category:** Troubleshooting\
**Created:** [March 22, 2023, 11:17pm UTC](https://community.taiga.io/t/duplicate-userstory-ref-after-jira-import/932 "2023-03-22T23:17:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RT-AP](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@RT-AP](https://community.taiga.io/u/RT-AP)\
**Post date:** [March 22, 2023, 11:17pm UTC](https://community.taiga.io/t/duplicate-userstory-ref-after-jira-import/932/1 "2023-03-22T23:17:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RT-AP](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@RT-AP](https://community.taiga.io/u/RT-AP)\
**Post date:** [March 31, 2023, 7:20pm UTC](https://community.taiga.io/t/duplicate-userstory-ref-after-jira-import/932/2 "2023-03-31T19:20:00Z")

</div>

@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](https://tree.taiga.io/project/taiga/issue/5165)) 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,
                             verbose_name=_("ref"))

```

---

<div class="post-metadata">

**Author:** ![david.barragan](https://dub1.discourse-cdn.com/flex017/user_avatar/community.taiga.io/david.barragan/32/293_2.png) [@david.barragan](https://community.taiga.io/u/david.barragan)\
**Post date:** [April 3, 2023, 10:55am UTC](https://community.taiga.io/t/duplicate-userstory-ref-after-jira-import/932/3 "2023-04-03T10:55:15Z")

</div>

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
> 
> ![image](https://europe1.discourse-cdn.com/flex017/uploads/taiga/original/1X/046144e24efe3706447d2cdb2638700ff2220922.jpeg)
> 
> `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.

---

<div class="post-metadata">

**Author:** ![RT-AP](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@RT-AP](https://community.taiga.io/u/RT-AP)\
**Post date:** [April 4, 2023, 7:22pm UTC](https://community.taiga.io/t/duplicate-userstory-ref-after-jira-import/932/4 "2023-04-04T19:22:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![RT-AP](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@RT-AP](https://community.taiga.io/u/RT-AP)\
**Post date:** [April 4, 2023, 8:32pm UTC](https://community.taiga.io/t/duplicate-userstory-ref-after-jira-import/932/5 "2023-04-04T20:32:22Z")

</div>

`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
