Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions backend/app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In current version of SQLModel, setting max_length will already create correct column type email VARCHAR(255)

)
is_active: bool = True
is_superuser: bool = False
full_name: str | None = Field(default=None, max_length=255)
Expand All @@ -18,20 +21,20 @@ class UserCreate(UserBase):


class UserRegister(SQLModel):
email: EmailStr = Field(max_length=255)
email: str = Field(max_length=255)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a point in this change. Moreover, it will allow creating users with invalid emails

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):
Expand All @@ -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
Expand Down Expand Up @@ -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]",
},
Comment on lines +88 to +95

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What kind of performance boost will this give?
I think we don't need it

)
owner: User | None = Relationship(back_populates="items")


# Properties to return via API, id is always required
Expand Down
Loading