Makhfi_AI / db /models /chat.py
Aasher's picture
fix: add index param into Column
4fefe49
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"}
)