You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I used the GitHub search to find a similar question and didn't find it.
I searched in the documentation/README.
I already searched in Google "How to do X" and didn't find any information.
I already read and followed all the tutorial in the docs/README and didn't find an answer.
Commit to Help
I commit to help with one of those options 👆
Example Code
@router.delete("/{user_id}", dependencies=[Depends(get_current_active_superuser)])
def delete_user(
session: SessionDep, current_user: CurrentUser, user_id: uuid.UUID
) -> Message:
"""
Delete a user.
"""
user = session.get(User, user_id)
if not user:
raise HTTPException(status_code=404, detail="User not found")
if user == current_user:
raise HTTPException(
status_code=403, detail="Super users are not allowed to delete themselves"
)
statement = delete(Item).where(col(Item.owner_id) == user_id)
session.exec(statement) # type: ignore
session.delete(user)
session.commit()
return Message(message="User deleted successfully")
# 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"
)
owner: User | None = Relationship(back_populates="items")
Description
I was reading the code in the template checking for best practices and how to build a FastAPI from scratch, and I realised that, the delete_user endpoint is manually deleting their items in the Items table. However, the table has a ON CASCADE constraint and therefore I believe not only this code is not necessary, but unadvised since, if in the future the constraint is modified, the records will be deleted anyway.
I dunno if this is intentional just to show how to handle the deletion of data or just an error nobody noticed, but I would love to fix it if it is an error.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
I was reading the code in the template checking for best practices and how to build a FastAPI from scratch, and I realised that, the delete_user endpoint is manually deleting their items in the Items table. However, the table has a ON CASCADE constraint and therefore I believe not only this code is not necessary, but unadvised since, if in the future the constraint is modified, the records will be deleted anyway.
I dunno if this is intentional just to show how to handle the deletion of data or just an error nobody noticed, but I would love to fix it if it is an error.
Operating System
macOS
Operating System Details
Sequoia 15.2
Python Version
3.12
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions