From 4df1d663363bed2ce7c59d451b48328cf60d1507 Mon Sep 17 00:00:00 2001 From: Bob Bobs Date: Wed, 10 Dec 2025 18:45:00 -0700 Subject: [PATCH 1/2] fix: when deleting tag remove all TagParent rows with it's id --- src/tagstudio/core/library/alchemy/library.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/tagstudio/core/library/alchemy/library.py b/src/tagstudio/core/library/alchemy/library.py index 1ce4fc85f..3dfd66415 100644 --- a/src/tagstudio/core/library/alchemy/library.py +++ b/src/tagstudio/core/library/alchemy/library.py @@ -1179,7 +1179,9 @@ def remove_tag(self, tag_id: int): session.flush() tag_parents = session.scalars( - select(TagParent).where(TagParent.parent_id == tag_id) + select(TagParent).where( + or_(TagParent.parent_id == tag_id, TagParent.child_id == tag_id) + ) ).all() for tag_parent in tag_parents: session.delete(tag_parent) From 7c0ab4ffed552aeaff0af49394e3c06cbbecc5ac Mon Sep 17 00:00:00 2001 From: Bob Bobs Date: Wed, 10 Dec 2025 19:25:37 -0700 Subject: [PATCH 2/2] delete TagEntry rows as well --- src/tagstudio/core/library/alchemy/library.py | 30 +++++++------------ 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/src/tagstudio/core/library/alchemy/library.py b/src/tagstudio/core/library/alchemy/library.py index 3dfd66415..ddb1a7bbe 100644 --- a/src/tagstudio/core/library/alchemy/library.py +++ b/src/tagstudio/core/library/alchemy/library.py @@ -1170,37 +1170,29 @@ def update_entry_path(self, entry_id: int | Entry, path: Path) -> bool: session.commit() return True - def remove_tag(self, tag_id: int): + def remove_tag(self, tag_id: int) -> bool: with Session(self.engine, expire_on_commit=False) as session: try: - aliases = session.scalars(select(TagAlias).where(TagAlias.tag_id == tag_id)) - for alias in aliases: - session.delete(alias) - session.flush() - - tag_parents = session.scalars( - select(TagParent).where( - or_(TagParent.parent_id == tag_id, TagParent.child_id == tag_id) + session.execute(delete(TagAlias).where(TagAlias.tag_id == tag_id)) + session.execute(delete(TagEntry).where(TagEntry.tag_id == tag_id)) + session.execute( + delete(TagParent).where( + or_(TagParent.child_id == tag_id, TagParent.parent_id == tag_id) ) - ).all() - for tag_parent in tag_parents: - session.delete(tag_parent) - session.flush() - - disam_stmt = ( + ) + session.execute( update(Tag) .where(Tag.disambiguation_id == tag_id) .values(disambiguation_id=None) ) - session.execute(disam_stmt) - session.flush() - - session.query(Tag).filter_by(id=tag_id).delete() + session.execute(delete(Tag).where(Tag.id == tag_id)) session.commit() except IntegrityError as e: logger.error(e) session.rollback() + return False + return True def update_field_position( self,