Skip to content

Commit dd01c7c

Browse files
authored
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
1 parent 86274ef commit dd01c7c

1 file changed

Lines changed: 12 additions & 18 deletions

File tree

src/tagstudio/core/library/alchemy/library.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,35 +1170,29 @@ def update_entry_path(self, entry_id: int | Entry, path: Path) -> bool:
11701170
session.commit()
11711171
return True
11721172

1173-
def remove_tag(self, tag_id: int):
1173+
def remove_tag(self, tag_id: int) -> bool:
11741174
with Session(self.engine, expire_on_commit=False) as session:
11751175
try:
1176-
aliases = session.scalars(select(TagAlias).where(TagAlias.tag_id == tag_id))
1177-
for alias in aliases:
1178-
session.delete(alias)
1179-
session.flush()
1180-
1181-
tag_parents = session.scalars(
1182-
select(TagParent).where(TagParent.parent_id == tag_id)
1183-
).all()
1184-
for tag_parent in tag_parents:
1185-
session.delete(tag_parent)
1186-
session.flush()
1187-
1188-
disam_stmt = (
1176+
session.execute(delete(TagAlias).where(TagAlias.tag_id == tag_id))
1177+
session.execute(delete(TagEntry).where(TagEntry.tag_id == tag_id))
1178+
session.execute(
1179+
delete(TagParent).where(
1180+
or_(TagParent.child_id == tag_id, TagParent.parent_id == tag_id)
1181+
)
1182+
)
1183+
session.execute(
11891184
update(Tag)
11901185
.where(Tag.disambiguation_id == tag_id)
11911186
.values(disambiguation_id=None)
11921187
)
1193-
session.execute(disam_stmt)
1194-
session.flush()
1195-
1196-
session.query(Tag).filter_by(id=tag_id).delete()
1188+
session.execute(delete(Tag).where(Tag.id == tag_id))
11971189
session.commit()
11981190

11991191
except IntegrityError as e:
12001192
logger.error(e)
12011193
session.rollback()
1194+
return False
1195+
return True
12021196

12031197
def update_field_position(
12041198
self,

0 commit comments

Comments
 (0)