From dd01c7cdcd761ca90c6b21c2701d1bd849711a36 Mon Sep 17 00:00:00 2001 From: TheBobBobs <84781603+TheBobBobs@users.noreply.github.com> Date: Sun, 18 Jan 2026 22:53:40 +0000 Subject: [PATCH] fix: when deleting tag remove all TagParent rows with it's id (#1250) * fix: when deleting tag remove all TagParent rows with it's id * delete TagEntry rows as well --- src/tagstudio/core/library/alchemy/library.py | 30 ++++++++----------- 1 file changed, 12 insertions(+), 18 deletions(-) diff --git a/src/tagstudio/core/library/alchemy/library.py b/src/tagstudio/core/library/alchemy/library.py index 1ce4fc85..ddb1a7bb 100644 --- a/src/tagstudio/core/library/alchemy/library.py +++ b/src/tagstudio/core/library/alchemy/library.py @@ -1170,35 +1170,29 @@ class Library: 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(TagParent.parent_id == tag_id) - ).all() - for tag_parent in tag_parents: - session.delete(tag_parent) - session.flush() - - disam_stmt = ( + 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) + ) + ) + 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,