From d60acd0d37776377102f892f2f26553735aeee16 Mon Sep 17 00:00:00 2001 From: maxime Date: Wed, 19 Nov 2025 10:59:48 -0800 Subject: [PATCH] Add ItemPurchase model to track purchases - Introduced a new `ItemPurchase` model to the SQL database, which includes fields for `id`, `item_id`, `user_id`, `quantity`, and `created_at`. - Implemented foreign key relationships for `item_id` and `user_id` with cascade deletion. - Added validation to ensure `quantity` is greater than or equal to 1. - Default value for `created_at` set to the current timestamp in UTC format. --- backend/app/models.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/backend/app/models.py b/backend/app/models.py index 2d060ba0b4..a68752bad8 100644 --- a/backend/app/models.py +++ b/backend/app/models.py @@ -1,4 +1,5 @@ import uuid +from datetime import datetime, timezone from pydantic import EmailStr from sqlmodel import Field, Relationship, SQLModel @@ -92,6 +93,19 @@ class ItemsPublic(SQLModel): count: int +# Database model, database table inferred from class name +class ItemPurchase(SQLModel, table=True): + id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) + item_id: uuid.UUID = Field( + foreign_key="item.id", nullable=False, ondelete="CASCADE" + ) + user_id: uuid.UUID = Field( + foreign_key="user.id", nullable=False, ondelete="CASCADE" + ) + quantity: int = Field(ge=1) + created_at: datetime = Field(default_factory=lambda: datetime.now(timezone.utc)) + + # Generic message class Message(SQLModel): message: str