Update models/user.py
Browse files- models/user.py +14 -2
models/user.py
CHANGED
|
@@ -1,5 +1,10 @@
|
|
| 1 |
-
from sqlmodel import SQLModel, Field
|
| 2 |
-
from typing import Optional
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
class UserBase(SQLModel):
|
| 5 |
username: str = Field(index=True, unique=True)
|
|
@@ -10,6 +15,13 @@ class UserBase(SQLModel):
|
|
| 10 |
class User(UserBase, table=True):
|
| 11 |
id: Optional[int] = Field(default=None, primary_key=True)
|
| 12 |
hashed_password: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
class UserCreate(UserBase):
|
| 15 |
password: str
|
|
|
|
| 1 |
+
from sqlmodel import SQLModel, Field, Relationship
|
| 2 |
+
from typing import Optional, List, TYPE_CHECKING
|
| 3 |
+
|
| 4 |
+
# Forward reference for type hinting to avoid circular imports
|
| 5 |
+
# This is crucial when models reference each other.
|
| 6 |
+
if TYPE_CHECKING:
|
| 7 |
+
from .chat import ChatSession # This assumes ChatSession is in chat.py
|
| 8 |
|
| 9 |
class UserBase(SQLModel):
|
| 10 |
username: str = Field(index=True, unique=True)
|
|
|
|
| 15 |
class User(UserBase, table=True):
|
| 16 |
id: Optional[int] = Field(default=None, primary_key=True)
|
| 17 |
hashed_password: str
|
| 18 |
+
|
| 19 |
+
# ------------ ADDED/CORRECTED SECTION ------------
|
| 20 |
+
# This defines the other side of the one-to-many relationship:
|
| 21 |
+
# A User can have many ChatSession objects.
|
| 22 |
+
# `back_populates="user"` links this to the `user` attribute in the ChatSession model.
|
| 23 |
+
chat_sessions: List["ChatSession"] = Relationship(back_populates="user")
|
| 24 |
+
# -------------------------------------------------
|
| 25 |
|
| 26 |
class UserCreate(UserBase):
|
| 27 |
password: str
|