Skip to content
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

Server Error on creating users for duplicated emails #15

Open
lugezz opened this issue Dec 9, 2022 · 1 comment
Open

Server Error on creating users for duplicated emails #15

lugezz opened this issue Dec 9, 2022 · 1 comment

Comments

@lugezz
Copy link

lugezz commented Dec 9, 2022

Server Error on creating users for duplicated emails, the api returns:

DETAIL: Key (email)=([email protected]) already exists.

To solve this, it should be included this code into create_user function in routers\user.py

this_email_count = db.query(models.User).filter(models.User.email == user.email).count()

    if this_email_count > 0:
        raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Email already used")
@mvarrone
Copy link

Hello! I noticed the same while I was studying this course.

To solve this problem, I decided to create a similar solution at the very beginning of this function:

query = db.query(models.User).filter(models.User.email == user.email)
if query.first() != None:
    raise HTTPException(status_code=status.HTTP_409_CONFLICT,
                        detail=f"Email {user.email} has already been used to create an user")

After this, password is hashed, user is validated, info is added, committed and refreshed and finally new_user is returned

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants