Creating Users Table via SQLAlchemy & FastAPI
First we will need to define our Table model in
models.py
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True, nullable=False)
email = Column(String, nullable=False, unique=True) # unique prevents email to be registered twice
password = Column(String, nullable=False)
created_at = Column(TIMESTAMP(timezone=True), nullable=False, server_default=text('now()'))We should set up a schema for the user registration in the
schemas.pyfileWe can use the
EmailStrvalidationThis requires the
email_validatorModule to be installed
Documentation and Additional info: Click here!
The schema should look something like this:
Now we will have to create a new path operation in FastAPI's
main.pyfile
Once we test this via postman we should get the response like this:
These is an issue with this, we should never send the PASSWORD back to the user
We need to define a user response in
schemas.py
We also need to update our response model in
main.py
We should get a proper response now with EMAIL and ID:
YOU SHOULD NEVER STORE THE PASSWORDS IN CLEAR TEXT AS IN THIS EXAMPLE
Click here to see how to HASH Passwords
Last updated