diff --git a/backend/app/models.py b/backend/app/models.py index 2389b4a532..345743917a 100644 --- a/backend/app/models.py +++ b/backend/app/models.py @@ -2,11 +2,14 @@ from pydantic import EmailStr from sqlmodel import Field, Relationship, SQLModel +from sqlalchemy import String # Shared properties class UserBase(SQLModel): - email: EmailStr = Field(unique=True, index=True, max_length=255) + email: EmailStr = Field( + unique=True, index=True, max_length=255, sa_type=String(255) + ) is_active: bool = True is_superuser: bool = False full_name: str | None = Field(default=None, max_length=255) @@ -18,20 +21,20 @@ class UserCreate(UserBase): class UserRegister(SQLModel): - email: EmailStr = Field(max_length=255) + email: str = Field(max_length=255) password: str = Field(min_length=8, max_length=40) full_name: str | None = Field(default=None, max_length=255) # Properties to receive via API on update, all are optional class UserUpdate(UserBase): - email: EmailStr | None = Field(default=None, max_length=255) # type: ignore + email: str | None = Field(default=None, max_length=255) # type: ignore password: str | None = Field(default=None, min_length=8, max_length=40) class UserUpdateMe(SQLModel): full_name: str | None = Field(default=None, max_length=255) - email: EmailStr | None = Field(default=None, max_length=255) + email: str | None = Field(default=None, max_length=255) class UpdatePassword(SQLModel): @@ -43,7 +46,14 @@ class UpdatePassword(SQLModel): class User(UserBase, table=True): id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) hashed_password: str - items: list["Item"] = Relationship(back_populates="owner", cascade_delete=True) + items: list["Item"] = Relationship( + back_populates="owner", + sa_relationship_kwargs={ + "cascade": "all, delete-orphan", + "primaryjoin": "User.id == Item.owner_id", + "foreign_keys": "[Item.owner_id]", + }, + ) # Properties to return via API, id is always required @@ -75,10 +85,15 @@ class ItemUpdate(ItemBase): # Database model, database table inferred from class name class Item(ItemBase, table=True): id: uuid.UUID = Field(default_factory=uuid.uuid4, primary_key=True) - owner_id: uuid.UUID = Field( - foreign_key="user.id", nullable=False, ondelete="CASCADE" + # Remove foreign_key constraint, but keep it indexed for performance + owner_id: uuid.UUID = Field(index=True, nullable=False) + owner: User | None = Relationship( + back_populates="items", + sa_relationship_kwargs={ + "primaryjoin": "User.id == Item.owner_id", + "foreign_keys": "[Item.owner_id]", + }, ) - owner: User | None = Relationship(back_populates="items") # Properties to return via API, id is always required