Remove Tag Unique Constraint Bug (#1519)

This commit is contained in:
Yuhong Sun 2024-05-26 15:10:56 -07:00 committed by GitHub
parent 97d058b8b2
commit 8688226003
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -76,17 +76,24 @@ def create_or_add_document_tag_list(
raise ValueError("Invalid Document, cannot attach Tags")
existing_tags_stmt = select(Tag).where(
Tag.tag_key == tag_key, Tag.tag_value.in_(tag_values), Tag.source == source
Tag.tag_key == tag_key,
Tag.tag_value.in_(valid_tag_values),
Tag.source == source,
)
existing_tags = list(db_session.execute(existing_tags_stmt).scalars().all())
existing_tag_values = {tag.tag_value for tag in existing_tags}
new_tags = []
for tag_value in tag_values:
for tag_value in valid_tag_values:
if tag_value not in existing_tag_values:
new_tag = Tag(tag_key=tag_key, tag_value=tag_value, source=source)
db_session.add(new_tag)
new_tags.append(new_tag)
existing_tag_values.add(tag_value)
logger.debug(
f"Created new tags: {', '.join([f'{tag.tag_key}:{tag.tag_value}' for tag in new_tags])}"
)
all_tags = existing_tags + new_tags