From 1cad157071c2214004ffb176b16e8957ef2f4da1 Mon Sep 17 00:00:00 2001 From: "Timothy J. Baek" Date: Fri, 11 Oct 2024 15:29:58 -0700 Subject: [PATCH] fix --- .../versions/1af9b942657b_migrate_tags.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/backend/open_webui/migrations/versions/1af9b942657b_migrate_tags.py b/backend/open_webui/migrations/versions/1af9b942657b_migrate_tags.py index 17ac74373..20f1d99c1 100644 --- a/backend/open_webui/migrations/versions/1af9b942657b_migrate_tags.py +++ b/backend/open_webui/migrations/versions/1af9b942657b_migrate_tags.py @@ -51,9 +51,22 @@ def upgrade(): delete_stmt = sa.delete(tag).where(tag.c.id == tag_id) conn.execute(delete_stmt) else: - update_stmt = sa.update(tag).where(tag.c.id == tag_id) - update_stmt = update_stmt.values(id=new_tag_id) - conn.execute(update_stmt) + # Check if the new_tag_id already exists in the database + existing_tag_query = sa.select(tag.c.id).where(tag.c.id == new_tag_id) + existing_tag_result = conn.execute(existing_tag_query).fetchone() + + if existing_tag_result: + # Handle duplicate case: the new_tag_id already exists + print( + f"Tag {new_tag_id} already exists. Removing current tag with ID {tag_id} to avoid duplicates." + ) + # Option 1: Delete the current tag if an update to new_tag_id would cause duplication + delete_stmt = sa.delete(tag).where(tag.c.id == tag_id) + conn.execute(delete_stmt) + else: + update_stmt = sa.update(tag).where(tag.c.id == tag_id) + update_stmt = update_stmt.values(id=new_tag_id) + conn.execute(update_stmt) # Add columns `pinned` and `meta` to 'chat' op.add_column("chat", sa.Column("pinned", sa.Boolean(), nullable=True))