Skip to content

Conversation

cubxxw
Copy link

@cubxxw cubxxw commented Apr 29, 2025

This pull request makes several changes to the backend/app/models.py file to enhance database modeling and improve compatibility with SQLAlchemy. The key updates include adjustments to field definitions, changes to relationships, and the removal of certain constraints.

Changes to field definitions:

  • Updated the email field in UserBase to explicitly use sa_type=String(255) for better SQLAlchemy compatibility.
  • Changed the email field type in UserRegister, UserUpdate, and UserUpdateMe from EmailStr to str for consistency and to avoid type-related issues.

Changes to relationships:

  • Modified the items relationship in the User model to include sa_relationship_kwargs for defining cascading behavior and specifying join conditions.
  • Updated the owner_id field in the Item model to remove the foreign_key constraint while keeping it indexed for performance. Added sa_relationship_kwargs to the owner relationship for explicit join conditions.

Copy link
Member

@YuriiMotov YuriiMotov left a comment

Choose a reason for hiding this comment

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

@cubxxw, thanks for your interest and efforts!

I think we don't need any of these changes

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)
Copy link
Member

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)


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

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

Comment on lines +88 to +95
# 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]",
},
Copy link
Member

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

@tiangolo
Copy link
Member

Yep, I think we don't currently need this, so I'll pass on this for now, but thanks for the interest! ☕

@tiangolo tiangolo closed this Sep 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants