11import uuid
2+ from datetime import datetime , timezone
23
34from pydantic import EmailStr
5+ from sqlalchemy import DateTime
46from sqlmodel import Field , Relationship , SQLModel
57
68
9+ def get_datetime_utc () -> datetime :
10+ return datetime .now (timezone .utc )
11+
12+
713# Shared properties
814class UserBase (SQLModel ):
915 email : EmailStr = Field (unique = True , index = True , max_length = 255 )
@@ -43,12 +49,17 @@ class UpdatePassword(SQLModel):
4349class User (UserBase , table = True ):
4450 id : uuid .UUID = Field (default_factory = uuid .uuid4 , primary_key = True )
4551 hashed_password : str
52+ created_at : datetime | None = Field (
53+ default_factory = get_datetime_utc ,
54+ sa_type = DateTime (timezone = True ), # type: ignore
55+ )
4656 items : list ["Item" ] = Relationship (back_populates = "owner" , cascade_delete = True )
4757
4858
4959# Properties to return via API, id is always required
5060class UserPublic (UserBase ):
5161 id : uuid .UUID
62+ created_at : datetime | None = None
5263
5364
5465class UsersPublic (SQLModel ):
@@ -75,6 +86,10 @@ class ItemUpdate(ItemBase):
7586# Database model, database table inferred from class name
7687class Item (ItemBase , table = True ):
7788 id : uuid .UUID = Field (default_factory = uuid .uuid4 , primary_key = True )
89+ created_at : datetime | None = Field (
90+ default_factory = get_datetime_utc ,
91+ sa_type = DateTime (timezone = True ), # type: ignore
92+ )
7893 owner_id : uuid .UUID = Field (
7994 foreign_key = "user.id" , nullable = False , ondelete = "CASCADE"
8095 )
@@ -85,6 +100,7 @@ class Item(ItemBase, table=True):
85100class ItemPublic (ItemBase ):
86101 id : uuid .UUID
87102 owner_id : uuid .UUID
103+ created_at : datetime | None = None
88104
89105
90106class ItemsPublic (SQLModel ):
0 commit comments