fix(tags): expire tag relationship after deleting all tagged objects#38163
Open
fix(tags): expire tag relationship after deleting all tagged objects#38163
Conversation
Fixes #36074 When removing all tagged objects from a tag, the Tag model's 'objects' relationship still held references to deleted TaggedObject instances. This caused a SQLAlchemy error when the tag was later added to the session: "Instance has been deleted. Use the make_transient() function to send this object back to the transient state." The fix calls db.session.expire(tag, ["objects"]) after deleting tagged objects to clear the stale references from the relationship, allowing the session to properly reload the relationship on next access. Added a regression test that verifies removing all tagged objects from a tag works without errors. Co-Authored-By: Claude <noreply@anthropic.com>
Contributor
Code Review Agent Run #d530e4Actionable Suggestions - 0Review Details
Bito Usage GuideCommands Type the following command in the pull request comment and save the comment.
Refer to the documentation for additional commands. Configuration This repository uses Documentation & Help |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
SUMMARY
Fixes #36074
When removing all tagged objects from a tag via the Edit Tag UI, users encountered the error "Error Updating Tag". The root cause was a SQLAlchemy session state issue where the Tag model's
objectsrelationship still held references to deletedTaggedObjectinstances after they were removed.Root Cause:
UpdateTagCommand.run()callsTagDAO.create_tag_relationship()create_tag_relationship()deletesTaggedObjectentries viadelete_tagged_object()tag.objectsrelationship still references the deleted objectsdb.session.add(self._model)is called, SQLAlchemy encounters the deleted objects and raises: "Instance has been deleted. Use the make_transient() function to send this object back to the transient state."Fix:
Call
db.session.expire(tag, ["objects"])after deleting tagged objects to clear the stale references from the relationship. This allows SQLAlchemy to properly reload the relationship on next access.BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF
Not applicable - this is a backend bug fix with no UI changes.
TESTING INSTRUCTIONS
Before fix: Step 6 would fail with "Error Updating Tag"
After fix: Step 6 succeeds and the tag is updated with no tagged objects
ADDITIONAL INFORMATION