I am building a FastAPI app and I have a database class in which ma doin all my database operations. The database that I use is MySQL.
Every time I try to fetch a user with null columns it was skipping everything that was null in my database. Also the result is a tuple so I cannot know which value belongs in which column.
I am trying to get a user from the database like this:
def get_user_by_id(id: int) -> dict:
with Session(engine) as session:
statement = select(
Users.id,
Users.username,
Users.email,
Users.firstname,
Users.lastname,
Users.birth_date,
func.coalesce(Roles.role, "").label('role'), )
.select_from(join(Users, Roles, Users.role == Roles.id))
.where(Users.id == id)
user = session.exec(statement).fetchone()
print (user)
user_dict = {
"id": user[0],
"username": user[1],
"email": user[2],
"firstname": user[3],
"lastname": user[4],
"birth_date": user[5],
"role": user[6]
}
return user_dict
the problem with that was it returned an item like that:
(18, ‘[email protected]’, ‘password’)
I tried doing this but didnt changed anything:
func.coalesce(Users.username, "").label('username'),
func.coalesce(Users.email, "").label('email'),
func.coalesce(Users.firstname, "").label('firstname'),
func.coalesce(Users.lastname, "").label('lastname'),
func.coalesce(Users.student_id, "").label('student_id'),
func.coalesce(Users.birth_date, "").label('birth_date'),
func.coalesce(Users.profile_picture, "").label('profile_picture'),
func.coalesce(Roles.role, "").label('role'),