File size: 1,131 Bytes
7c1504c
 
 
 
 
 
 
afc35b3
 
 
 
 
 
 
 
 
 
7c1504c
 
 
 
 
 
 
afc35b3
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from sqlmodel import SQLModel, Field, Relationship
from typing import Optional, List, TYPE_CHECKING

# Forward reference for type hinting to avoid circular imports
# This is crucial when models reference each other.
if TYPE_CHECKING:
    from .chat import ChatSession # This assumes ChatSession is in chat.py

class UserBase(SQLModel):
    username: str = Field(index=True, unique=True)
    email: Optional[str] = Field(default=None, index=True, unique=True)
    full_name: Optional[str] = None
    disabled: bool = False

class User(UserBase, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    hashed_password: str
    
    # ------------ ADDED/CORRECTED SECTION ------------
    # This defines the other side of the one-to-many relationship:
    # A User can have many ChatSession objects.
    # `back_populates="user"` links this to the `user` attribute in the ChatSession model.
    chat_sessions: List["ChatSession"] = Relationship(back_populates="user")
    # -------------------------------------------------

class UserCreate(UserBase):
    password: str

class UserPublic(UserBase):
    id: int