Search code examples
pythonrestflasksqlalchemymarshmallow

Flask back-end returning invalid data


I'm building a Flask backend. I have this route that should return the ID that matches the email

@app.route('/login', methods=['POST'])
def login():
    email = request.json['data']
    id = session.query(Users).filter_by(email=Users.estudent_email)
    result = users_schema.dump(id)
    return jsonify(result)

I'm currently sending it this data

{
    "data": "name.lastname@email.com"
}

But it returns a whole bunch of data which is incorrect. It should return a single ID from the database that is assigned to the user that holds the email. What might be the cause? I'm using Marshmallow and SQLAlchemy ORM.


Solution

  • The problem is probably in this line:

    id = session.query(Users).filter_by(email=Users.estudent_email)
    

    You seem to have the equality check the wrong way around. Users.estudent_email references the entire field of the database, and email is a named argument in filter_by, not email that you defined in the previous line.

    I can't test but I'm thinking you want:

    id = session.query(Users).filter(Users.estudent_email==email)
    

    or

    id = session.query(Users).filter_by(email=email)
    

    As for the serialization, and result = users_schema.dump(id), I have no idea because there is not enough context given to be sure how to make that work.