File size: 1,146 Bytes
f6462a2
b68a6a7
f6462a2
38ccc4e
b68a6a7
f6462a2
 
 
 
b68a6a7
 
 
 
f6462a2
 
 
 
b68a6a7
 
 
 
 
 
f6462a2
b68a6a7
 
 
 
4fefe49
 
 
f6462a2
b68a6a7
f6462a2
 
 
 
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
31
32
33
34
35
36
37
import uuid
from datetime import datetime, timezone
from sqlmodel import Field, Relationship, SQLModel
from typing import TYPE_CHECKING
from sqlalchemy import Column, TIMESTAMP

if TYPE_CHECKING:
    from .message import Message

# Helper function for aware UTC timestamps
def aware_utcnow():
    return datetime.now(timezone.utc)

class Chat(SQLModel, table=True):
    id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True, index=True)
    user_id: uuid.UUID = Field(index=True)
    title: str = Field(default="New Chat", max_length=100)
    
    # Use aware timestamps and TIMESTAMP WITH TIME ZONE
    created_at: datetime = Field(
        default_factory=aware_utcnow,
        sa_column=Column(TIMESTAMP(timezone=True), nullable=False)
    )
    updated_at: datetime = Field(
        default_factory=aware_utcnow, 
        sa_column=Column(
            TIMESTAMP(timezone=True), 
            nullable=False,
            onupdate=aware_utcnow,
            index=True
        )
    )

    messages: list["Message"] = Relationship(
        back_populates="chat",
        sa_relationship_kwargs={"cascade": "all, delete-orphan"}
    )