Update models/chat.py
Browse files- models/chat.py +22 -13
models/chat.py
CHANGED
@@ -1,34 +1,43 @@
|
|
1 |
from sqlmodel import SQLModel, Field, Relationship
|
2 |
-
from typing import Optional, List
|
3 |
from datetime import datetime
|
4 |
|
|
|
|
|
|
|
|
|
5 |
class ChatSessionBase(SQLModel):
|
6 |
-
user_id: int = Field(foreign_key="user.id")
|
7 |
start_time: datetime = Field(default_factory=datetime.utcnow)
|
8 |
-
title: Optional[str] = None
|
9 |
|
10 |
class ChatSession(ChatSessionBase, table=True):
|
11 |
id: Optional[int] = Field(default=None, primary_key=True)
|
12 |
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
14 |
messages: List["ChatMessage"] = Relationship(back_populates="session")
|
15 |
-
|
16 |
-
# Add to User model in user.py:
|
17 |
-
# chat_sessions: List["ChatSession"] = Relationship(back_populates="user")
|
18 |
-
|
19 |
|
20 |
class ChatMessageBase(SQLModel):
|
|
|
|
|
21 |
session_id: int = Field(foreign_key="chatsession.id")
|
22 |
-
role: str
|
23 |
content: str
|
24 |
timestamp: datetime = Field(default_factory=datetime.utcnow)
|
25 |
-
tool_call_id: Optional[str] = None
|
26 |
-
tool_name: Optional[str] = None
|
27 |
-
|
28 |
|
29 |
class ChatMessage(ChatMessageBase, table=True):
|
30 |
id: Optional[int] = Field(default=None, primary_key=True)
|
31 |
-
|
|
|
|
|
32 |
|
33 |
class ChatMessageCreate(ChatMessageBase):
|
34 |
pass
|
|
|
1 |
from sqlmodel import SQLModel, Field, Relationship
|
2 |
+
from typing import Optional, List, TYPE_CHECKING # Ensure TYPE_CHECKING is imported
|
3 |
from datetime import datetime
|
4 |
|
5 |
+
# Forward reference for type hinting to avoid circular imports
|
6 |
+
if TYPE_CHECKING:
|
7 |
+
from .user import User # For the 'user' attribute in ChatSession
|
8 |
+
|
9 |
class ChatSessionBase(SQLModel):
|
10 |
+
user_id: int = Field(foreign_key="user.id") # Foreign key to the 'user' table's 'id' column
|
11 |
start_time: datetime = Field(default_factory=datetime.utcnow)
|
12 |
+
title: Optional[str] = None
|
13 |
|
14 |
class ChatSession(ChatSessionBase, table=True):
|
15 |
id: Optional[int] = Field(default=None, primary_key=True)
|
16 |
|
17 |
+
# ------------ RELATIONSHIP DEFINITION ------------
|
18 |
+
# A ChatSession belongs to one User.
|
19 |
+
# `back_populates="chat_sessions"` links this to the `chat_sessions` attribute in the User model.
|
20 |
+
user: Optional["User"] = Relationship(back_populates="chat_sessions")
|
21 |
+
|
22 |
+
# A ChatSession can have many ChatMessage objects.
|
23 |
messages: List["ChatMessage"] = Relationship(back_populates="session")
|
24 |
+
# -------------------------------------------------
|
|
|
|
|
|
|
25 |
|
26 |
class ChatMessageBase(SQLModel):
|
27 |
+
# Foreign key to the 'chatsession' table's 'id' column.
|
28 |
+
# SQLModel infers table names from class names (lowercase).
|
29 |
session_id: int = Field(foreign_key="chatsession.id")
|
30 |
+
role: str
|
31 |
content: str
|
32 |
timestamp: datetime = Field(default_factory=datetime.utcnow)
|
33 |
+
tool_call_id: Optional[str] = None
|
34 |
+
tool_name: Optional[str] = None
|
|
|
35 |
|
36 |
class ChatMessage(ChatMessageBase, table=True):
|
37 |
id: Optional[int] = Field(default=None, primary_key=True)
|
38 |
+
|
39 |
+
# A ChatMessage belongs to one ChatSession.
|
40 |
+
session: Optional["ChatSession"] = Relationship(back_populates="messages")
|
41 |
|
42 |
class ChatMessageCreate(ChatMessageBase):
|
43 |
pass
|