byc
byc

Reputation: 66

Flask-Sqlalchemy seemingly "caching" queries

I'm writing a signup/login script for Flask, and I am using flask-sqlalchemy. To sign a user up, I INSERT their details into the db through flask-sqlalchemy, then commit the change. However, when they try to login, which fetches their details from the db, I get a NoneType error, indicating that the entry I am trying to find is not present.

I am using MySQL 5.5 and the latest version of Flask and Flask-Sqlalchemy.

Error:

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

Code:

 newuser = User(username='username', email='email', valid=1, password='hashpass', rkey=rkey, score=0, ip=uip)
 db.session.add(newuser)
 db.session.commit()
 pwhash = User.query.filter_by(username='username').first().password
 return str(pwhash)

Model

 class User(db.Model):
    __tablename__ = 'users'
    uid = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(50))
    password = db.Column(db.Text)
    email = db.Column(db.String(120))
    rkey = db.Column(db.String(50))
    role = db.Column(db.String(20))
    valid = db.Column(db.Boolean)
    ip = db.Column(db.String(110))
    lastip = db.Column(db.String(110))
    score = db.Column(db.Integer)


    def __repr__(self):
        return '<User %r>' % self.username

Upvotes: 2

Views: 996

Answers (1)

kylieCatt
kylieCatt

Reputation: 11039

I believe your query should be:

pwhash = User.query.filter_by(User.username='username').first().password

Upvotes: 1

Related Questions