diff --git a/backend/open_webui/migrations/versions/8452d01d26d7_add_chat_message_table.py b/backend/open_webui/migrations/versions/8452d01d26d7_add_chat_message_table.py index 3b178e60cb..e774ff11d2 100644 --- a/backend/open_webui/migrations/versions/8452d01d26d7_add_chat_message_table.py +++ b/backend/open_webui/migrations/versions/8452d01d26d7_add_chat_message_table.py @@ -137,7 +137,7 @@ def upgrade() -> None: try: conn.execute( sa.insert(chat_message_table).values( - id=message_id, + id=f"{chat_id}-{message_id}", chat_id=chat_id, user_id=user_id, role=role, diff --git a/backend/open_webui/models/chat_messages.py b/backend/open_webui/models/chat_messages.py index bc7508d45e..88d1c9ff7c 100644 --- a/backend/open_webui/models/chat_messages.py +++ b/backend/open_webui/models/chat_messages.py @@ -137,7 +137,10 @@ class ChatMessageTable: now = int(time.time()) timestamp = data.get("timestamp", now) - existing = db.get(ChatMessage, message_id) + # Use composite ID: {chat_id}-{message_id} + composite_id = f"{chat_id}-{message_id}" + + existing = db.get(ChatMessage, composite_id) if existing: # Update existing if "role" in data: @@ -183,7 +186,7 @@ class ChatMessageTable: info = data.get("info", {}) usage = info.get("usage") if info else None message = ChatMessage( - id=message_id, + id=composite_id, chat_id=chat_id, user_id=user_id, role=data.get("role", "user"),