-
Notifications
You must be signed in to change notification settings - Fork 11
Fix duplicate TaxaList names causing MultipleObjectsReturned #1108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3609,6 +3609,44 @@ def save(self, update_calculated_fields=True, *args, **kwargs): | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self.update_calculated_fields(save=True) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| class TaxaListQuerySet(BaseQuerySet): | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| def get_or_create_for_project( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| self, name: str, project: "Project | None" = None, **defaults | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) -> tuple["TaxaList", bool]: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Get or create a TaxaList with uniqueness scoped to project. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - If project is None: looks for/creates a global list (no project associations) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| - If project is provided: looks for/creates a list associated with that project | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Returns: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Tuple of (TaxaList, created: bool) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if project is None: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Global list: find list with this name that has no project associations | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| qs = self.filter(name=name).annotate(project_count=models.Count("projects")).filter(project_count=0) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Project-specific: find list with this name in this project | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| qs = self.filter(name=name, projects=project) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| try: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return qs.get(), False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except self.model.DoesNotExist: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| taxa_list = self.create(name=name, **defaults) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if project: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| taxa_list.projects.add(project) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return taxa_list, True | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| except self.model.MultipleObjectsReturned: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| # Handle existing duplicates gracefully - return the oldest one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| taxa_list = qs.order_by("created_at").first() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| assert taxa_list is not None # We know there's at least one | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return taxa_list, False | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+3625
to
+3643
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if project is None: | |
| # Global list: find list with this name that has no project associations | |
| qs = self.filter(name=name).annotate(project_count=models.Count("projects")).filter(project_count=0) | |
| else: | |
| # Project-specific: find list with this name in this project | |
| qs = self.filter(name=name, projects=project) | |
| try: | |
| return qs.get(), False | |
| except self.model.DoesNotExist: | |
| taxa_list = self.create(name=name, **defaults) | |
| if project: | |
| taxa_list.projects.add(project) | |
| return taxa_list, True | |
| except self.model.MultipleObjectsReturned: | |
| # Handle existing duplicates gracefully - return the oldest one | |
| taxa_list = qs.order_by("created_at").first() | |
| assert taxa_list is not None # We know there's at least one | |
| return taxa_list, False | |
| with transaction.atomic(): | |
| if project is None: | |
| # Global list: find list with this name that has no project associations | |
| qs = ( | |
| self.filter(name=name) | |
| .annotate(project_count=models.Count("projects")) | |
| .filter(project_count=0) | |
| ) | |
| else: | |
| # Project-specific: find list with this name in this project | |
| qs = self.filter(name=name, projects=project) | |
| try: | |
| # Lock matching rows, if any, to avoid concurrent modifications | |
| return qs.select_for_update().get(), False | |
| except self.model.DoesNotExist: | |
| try: | |
| taxa_list = self.create(name=name, **defaults) | |
| except IntegrityError: | |
| # Another transaction may have created the same TaxaList concurrently. | |
| # Re-fetch and return the existing instance. | |
| taxa_list = qs.get() | |
| return taxa_list, False | |
| else: | |
| if project: | |
| taxa_list.projects.add(project) | |
| return taxa_list, True | |
| except self.model.MultipleObjectsReturned: | |
| # Handle existing duplicates gracefully - return the oldest one | |
| taxa_list = qs.order_by("created_at").first() | |
| assert taxa_list is not None # We know there's at least one | |
| return taxa_list, False |
Copilot
AI
Feb 27, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new get_or_create_for_project method lacks test coverage. Consider adding tests that verify: 1) Creating a new global list (project=None), 2) Creating a new project-specific list, 3) Retrieving an existing global list, 4) Retrieving an existing project-specific list, 5) Handling duplicate names gracefully by returning the oldest, and 6) Ensuring that global lists and project-specific lists with the same name are properly distinguished.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
**defaultsparameter is documented in the type signature but not used in the 'get' path (line 3633). This is intentional since defaults only apply during creation, but could be confusing. Consider adding a docstring parameter description explaining thatdefaultsare only used when creating a new TaxaList, not when retrieving an existing one.