-
-
Notifications
You must be signed in to change notification settings - Fork 7.2k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
@@ -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]", | ||
}, | ||
Comment on lines
+88
to
+95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What kind of performance boost will this give? |
||
) | ||
owner: User | None = Relationship(back_populates="items") | ||
|
||
|
||
# Properties to return via API, id is always required | ||
|
There was a problem hiding this comment.
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 typeemail VARCHAR(255)