Skip to content

Enhance Database Modeling and SQLAlchemy Compatibility #1593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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)
)
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)
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]",
},
)
owner: User | None = Relationship(back_populates="items")


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