1

I am creating a Flask Application that connects to a locally-hosted MySQL database (Using SQL-Alchemy ORM). When a user creates an account, I have a method is_taken and returns True or False depending on if a user with that username already exists.

Here is the method:

def is_taken(username):
    q = session.query(User).filter(User.username == username).first()
    return not (q is None)

Although not on a regular basis, the following error occurs at least once a day:

StatementError: (sqlalchemy.exc.InvalidRequestError) Can't reconnect until invalid transaction is rolled back [SQL: u'SELECT users.uid AS users_uid, users.username AS users_username, users.fullname AS users_fullname, users.password AS users_password, users.score AS users_score, users.totalattempted AS users_totalattempted, users.totalcorrect AS users_totalcorrect, users.settings AS users_settings \nFROM users \nWHERE users.username = %s \n LIMIT %s'] [parameters: [immutabledict({})]]

The error is triggered specifically on:

q = session.query(User).filter(User.username == username).first()

I appreciate the help!

Barmar
  • 741,623
  • 53
  • 500
  • 612
rawells14
  • 59
  • 5

1 Answers1

2

you've had a invalid transaction before executing this query. first of all I suggest you to find the problem of previous query that led to this problem. and for fixing this problem you execute session.rollback() before running the query.

mehdy
  • 3,174
  • 4
  • 23
  • 44
  • So in other words, an invalid transaction would cause the method to return an immutable dict? – rawells14 Dec 20 '15 at 18:19
  • 2
    @rawells14 no! the invalid transaction prevents making another transaction by raising an error until you clear the session. – mehdy Apr 24 '16 at 19:18