From 84c60b0dab8e20541af6699d22c2a18707422d3f Mon Sep 17 00:00:00 2001 From: weer0026 Date: Wed, 15 Oct 2025 14:52:44 +0800 Subject: [PATCH 1/2] update chat history model: add project id --- ...3a9b_modify_chat_history_add_project_id.py | 36 +++++++++++++++++++ server/app/model/chat/chat_history.py | 13 ++++++- 2 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 server/alembic/versions/2025_10_15_1446-eec7242b3a9b_modify_chat_history_add_project_id.py diff --git a/server/alembic/versions/2025_10_15_1446-eec7242b3a9b_modify_chat_history_add_project_id.py b/server/alembic/versions/2025_10_15_1446-eec7242b3a9b_modify_chat_history_add_project_id.py new file mode 100644 index 000000000..0c721299f --- /dev/null +++ b/server/alembic/versions/2025_10_15_1446-eec7242b3a9b_modify_chat_history_add_project_id.py @@ -0,0 +1,36 @@ +"""modify_chat_history_add_project_id + +Revision ID: eec7242b3a9b +Revises: d74ab2a44600 +Create Date: 2025-10-15 14:46:47.904254 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa +import sqlmodel.sql.sqltypes + + +# revision identifiers, used by Alembic. +revision: str = "eec7242b3a9b" +down_revision: Union[str, None] = "d74ab2a44600" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + """Upgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + op.add_column("chat_history", sa.Column("project_id", sqlmodel.sql.sqltypes.AutoString(), nullable=True)) + op.create_index(op.f("ix_chat_history_project_id"), "chat_history", ["project_id"], unique=True) + # ### end Alembic commands ### + + +def downgrade() -> None: + """Downgrade schema.""" + # ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f("ix_chat_history_project_id"), table_name="chat_history") + op.drop_column("chat_history", "project_id") + # ### end Alembic commands ### diff --git a/server/app/model/chat/chat_history.py b/server/app/model/chat/chat_history.py index e351e34ae..06180c6e9 100644 --- a/server/app/model/chat/chat_history.py +++ b/server/app/model/chat/chat_history.py @@ -4,7 +4,7 @@ from typing import Optional from enum import IntEnum from sqlalchemy_utils import ChoiceType from app.model.abstract.model import AbstractModel, DefaultTimes -from pydantic import BaseModel +from pydantic import BaseModel, model_validator class ChatStatus(IntEnum): @@ -16,6 +16,7 @@ class ChatHistory(AbstractModel, DefaultTimes, table=True): id: int = Field(default=None, primary_key=True) user_id: int = Field(index=True) task_id: str = Field(index=True, unique=True) + project_id: str = Field(index=True, unique=True, nullable=True) question: str language: str model_platform: str @@ -34,6 +35,7 @@ class ChatHistory(AbstractModel, DefaultTimes, table=True): class ChatHistoryIn(BaseModel): task_id: str + project_id: str | None = None user_id: int | None = None question: str language: str @@ -54,6 +56,7 @@ class ChatHistoryIn(BaseModel): class ChatHistoryOut(BaseModel): id: int task_id: str + project_id: str | None = None question: str language: str model_platform: str @@ -68,9 +71,17 @@ class ChatHistoryOut(BaseModel): tokens: int status: int + @model_validator(mode="after") + def fill_project_id_from_task_id(self): + """fill by task_id when project_id is None""" + if self.project_id is None: + self.project_id = self.task_id + return self + class ChatHistoryUpdate(BaseModel): project_name: str | None = None summary: str | None = None tokens: int | None = None status: int | None = None + project_id: str | None = None From cb66a9c36703320c2ca711b6521536a4db8751da Mon Sep 17 00:00:00 2001 From: Wendong-Fan Date: Sat, 18 Oct 2025 21:33:59 +0800 Subject: [PATCH 2/2] chore: update project id to unique=False --- ...0_15_1446-eec7242b3a9b_modify_chat_history_add_project_id.py | 2 +- server/app/model/chat/chat_history.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/alembic/versions/2025_10_15_1446-eec7242b3a9b_modify_chat_history_add_project_id.py b/server/alembic/versions/2025_10_15_1446-eec7242b3a9b_modify_chat_history_add_project_id.py index 0c721299f..aaa7626dc 100644 --- a/server/alembic/versions/2025_10_15_1446-eec7242b3a9b_modify_chat_history_add_project_id.py +++ b/server/alembic/versions/2025_10_15_1446-eec7242b3a9b_modify_chat_history_add_project_id.py @@ -24,7 +24,7 @@ def upgrade() -> None: """Upgrade schema.""" # ### commands auto generated by Alembic - please adjust! ### op.add_column("chat_history", sa.Column("project_id", sqlmodel.sql.sqltypes.AutoString(), nullable=True)) - op.create_index(op.f("ix_chat_history_project_id"), "chat_history", ["project_id"], unique=True) + op.create_index(op.f("ix_chat_history_project_id"), "chat_history", ["project_id"], unique=False) # ### end Alembic commands ### diff --git a/server/app/model/chat/chat_history.py b/server/app/model/chat/chat_history.py index 06180c6e9..6dd7775a1 100644 --- a/server/app/model/chat/chat_history.py +++ b/server/app/model/chat/chat_history.py @@ -16,7 +16,7 @@ class ChatHistory(AbstractModel, DefaultTimes, table=True): id: int = Field(default=None, primary_key=True) user_id: int = Field(index=True) task_id: str = Field(index=True, unique=True) - project_id: str = Field(index=True, unique=True, nullable=True) + project_id: str = Field(index=True, unique=False, nullable=True) question: str language: str model_platform: str