2
votes

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
1
when do you add this user to the database? What is the context? Can you do a print(db.query.all()) and see if the database did anything with it? - corvid
When running print(db.query.all()), I get File "<debugger>", line 1, in <module> print(db.query.all()) AttributeError: 'SQLAlchemy' object has no attribute 'query'. I set db = SQLAlchemy() in the beginning of the app, not sure if that makes a difference. - byc
errr sorry, I meant User.query.all() - corvid
It echoes a list in this format: [<User u'cydrobolt'>, <User u'admin'> ...], but the new user I registered is not there. - byc
This question seems to ask about the same issue: stackoverflow.com/questions/15406623/… - Nathan Wailes

1 Answers

1
votes

I believe your query should be:

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