Search code examples
flasksqlalchemyflask-sqlalchemy

AttributeError: 'Query' object has no attribute 'password'


So I am trying to use bcrypt in my Flask app to check if my passwords match. But after running my code I get AttributeError: 'Query' object has no attribute 'password' and it seems to me that it has some problem getting password from database but i don't know why.

Here is my code:

auth = request.authorization
local_session = Session(bind=engine)
user = local_session.query(User).filter_by(email=auth.username)

if bcrypt.checkpw(auth.password.encode('utf-8'), user.password.encode('utf-8')):
   some stuff...

My databse:

class User(Base):
    __tablename__ = "users"
    password = Column(String(100))

So I checked names of my column and name I use in code but it is the same. I really don't know what is wrong here.


Solution

  • So apparently I didn't run the Query and it needs either .all() or .first().

    So in this situations it should look like this:

    user = local_session.query(User).filter_by(email=auth.username).first()
    

    This solves the problem and works fine.