Skip to content

Commit 5c7c34d

Browse files
committed
fix(AUTOFILL macro): error when trying to add tag with name that already exists
1 parent 191ed29 commit 5c7c34d

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -726,7 +726,7 @@ def add_entry_field_type(
726726
if value:
727727
assert isinstance(value, list)
728728
for tag in value:
729-
field_model.tags.add(Tag(name=tag))
729+
field_model.tags.add(self.get_tag_by_name(tag) or Tag(name=tag))
730730

731731
elif field.type == FieldTypeEnum.DATETIME:
732732
field_model = DatetimeField(
@@ -870,6 +870,15 @@ def get_tag(self, tag_id: int) -> Tag:
870870

871871
return tag
872872

873+
def get_tag_by_name(self, tag_name: str) -> Tag | None:
874+
with Session(self.engine) as session:
875+
statement = (
876+
select(Tag)
877+
.outerjoin(TagAlias)
878+
.where(or_(Tag.name == tag_name, TagAlias.name == tag_name))
879+
)
880+
return session.scalar(statement)
881+
873882
def get_alias(self, tag_id: int, alias_id: int) -> TagAlias:
874883
with Session(self.engine) as session:
875884
alias_query = select(TagAlias).where(TagAlias.id == alias_id, TagAlias.tag_id == tag_id)

0 commit comments

Comments
 (0)