22
33from pydantic import EmailStr
44from sqlmodel import Field , Relationship , SQLModel
5+ from sqlalchemy import String
56
67
78# Shared properties
89class UserBase (SQLModel ):
9- email : EmailStr = Field (unique = True , index = True , max_length = 255 )
10+ email : EmailStr = Field (
11+ unique = True , index = True , max_length = 255 , sa_type = String (255 )
12+ )
1013 is_active : bool = True
1114 is_superuser : bool = False
1215 full_name : str | None = Field (default = None , max_length = 255 )
@@ -18,20 +21,20 @@ class UserCreate(UserBase):
1821
1922
2023class UserRegister (SQLModel ):
21- email : EmailStr = Field (max_length = 255 )
24+ email : str = Field (max_length = 255 )
2225 password : str = Field (min_length = 8 , max_length = 40 )
2326 full_name : str | None = Field (default = None , max_length = 255 )
2427
2528
2629# Properties to receive via API on update, all are optional
2730class UserUpdate (UserBase ):
28- email : EmailStr | None = Field (default = None , max_length = 255 ) # type: ignore
31+ email : str | None = Field (default = None , max_length = 255 ) # type: ignore
2932 password : str | None = Field (default = None , min_length = 8 , max_length = 40 )
3033
3134
3235class UserUpdateMe (SQLModel ):
3336 full_name : str | None = Field (default = None , max_length = 255 )
34- email : EmailStr | None = Field (default = None , max_length = 255 )
37+ email : str | None = Field (default = None , max_length = 255 )
3538
3639
3740class UpdatePassword (SQLModel ):
@@ -43,7 +46,14 @@ class UpdatePassword(SQLModel):
4346class User (UserBase , table = True ):
4447 id : uuid .UUID = Field (default_factory = uuid .uuid4 , primary_key = True )
4548 hashed_password : str
46- items : list ["Item" ] = Relationship (back_populates = "owner" , cascade_delete = True )
49+ items : list ["Item" ] = Relationship (
50+ back_populates = "owner" ,
51+ sa_relationship_kwargs = {
52+ "cascade" : "all, delete-orphan" ,
53+ "primaryjoin" : "User.id == Item.owner_id" ,
54+ "foreign_keys" : "[Item.owner_id]" ,
55+ },
56+ )
4757
4858
4959# Properties to return via API, id is always required
@@ -75,10 +85,15 @@ class ItemUpdate(ItemBase):
7585# Database model, database table inferred from class name
7686class Item (ItemBase , table = True ):
7787 id : uuid .UUID = Field (default_factory = uuid .uuid4 , primary_key = True )
78- owner_id : uuid .UUID = Field (
79- foreign_key = "user.id" , nullable = False , ondelete = "CASCADE"
88+ # Remove foreign_key constraint, but keep it indexed for performance
89+ owner_id : uuid .UUID = Field (index = True , nullable = False )
90+ owner : User | None = Relationship (
91+ back_populates = "items" ,
92+ sa_relationship_kwargs = {
93+ "primaryjoin" : "User.id == Item.owner_id" ,
94+ "foreign_keys" : "[Item.owner_id]" ,
95+ },
8096 )
81- owner : User | None = Relationship (back_populates = "items" )
8297
8398
8499# Properties to return via API, id is always required
0 commit comments