Forums

All SQLAlchemy operations throws "KeyError: 140291498702720"

I've been battling with this for an hour now, and can't seem to figure out what's going on...

I just deployed my app, and everything works fine - except for SQLAlchemy database operations. When i go to register a new user (or login, or anything else that uses a SQLa model), i get a 500 Server Error front-end, and the KeyError pasted in the title in my error log (number changes every time).

Here's a full stack trace, thanks in advance! Looking forward to getting things up and running:


2018-04-20 20:05:05,876: Traceback (most recent call last):
2018-04-20 20:05:05,876:   File "/home/dellitsni/.virtualenvs/konsent_3/lib/python3.5/site-packages/sqlalchemy/util/_collections.py", line 999, in __call__
2018-04-20 20:05:05,876:     return self.registry[key]
2018-04-20 20:05:05,876: KeyError: 140291498702720
2018-04-20 20:05:05,876: 
2018-04-20 20:05:05,876: During handling of the above exception, another exception occurred:
2018-04-20 20:05:05,877: 
2018-04-20 20:05:05,877: Traceback (most recent call last):
2018-04-20 20:05:05,877:   File "/home/dellitsni/.virtualenvs/konsent_3/lib/python3.5/site-packages/flask/app.py", line 1982, in wsgi_app
2018-04-20 20:05:05,877:     response = self.full_dispatch_request()
2018-04-20 20:05:05,877:   File "/home/dellitsni/.virtualenvs/konsent_3/lib/python3.5/site-packages/flask/app.py", line 1614, in full_dispatch_request
2018-04-20 20:05:05,877:     rv = self.handle_user_exception(e)
2018-04-20 20:05:05,877:   File "/home/dellitsni/.virtualenvs/konsent_3/lib/python3.5/site-packages/flask/app.py", line 1517, in handle_user_exception
2018-04-20 20:05:05,877:     reraise(exc_type, exc_value, tb)
2018-04-20 20:05:05,877:   File "/home/dellitsni/.virtualenvs/konsent_3/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
2018-04-20 20:05:05,878:     raise value
2018-04-20 20:05:05,878:   File "/home/dellitsni/.virtualenvs/konsent_3/lib/python3.5/site-packages/flask/app.py", line 1612, in full_dispatch_request
2018-04-20 20:05:05,878:     rv = self.dispatch_request()
2018-04-20 20:05:05,878:   File "/home/dellitsni/.virtualenvs/konsent_3/lib/python3.5/site-packages/flask/app.py", line 1598, in dispatch_request
2018-04-20 20:05:05,878:     return self.view_functions[rule.endpoint](**req.view_args)
2018-04-20 20:05:05,878:   File "/home/dellitsni/Konsent/konsent/__init__.py", line 321, in register
2018-04-20 20:05:05,878:     user_exists = User.query.filter(User.username == username).first()
2018-04-20 20:05:05,878:   File "/home/dellitsni/.virtualenvs/konsent_3/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py", line 514, in __get__
2018-04-20 20:05:05,878:     return type.query_class(mapper, session=self.sa.session())
2018-04-20 20:05:05,878:   File "/home/dellitsni/.virtualenvs/konsent_3/lib/python3.5/site-packages/sqlalchemy/orm/scoping.py", line 74, in __call__
2018-04-20 20:05:05,879:     return self.registry()
2018-04-20 20:05:05,879:   File "/home/dellitsni/.virtualenvs/konsent_3/lib/python3.5/site-packages/sqlalchemy/util/_collections.py", line 1001, in __call__
2018-04-20 20:05:05,879:     return self.registry.setdefault(key, self.createfunc())
2018-04-20 20:05:05,879:   File "/home/dellitsni/.virtualenvs/konsent_3/lib/python3.5/site-packages/sqlalchemy/orm/session.py", line 2950, in __call__
2018-04-20 20:05:05,879:     return self.class_(**local_kw)
2018-04-20 20:05:05,879:   File "/home/dellitsni/.virtualenvs/konsent_3/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py", line 142, in __init__
2018-04-20 20:05:05,879:     track_modifications = app.config['SQLALCHEMY_TRACK_MODIFICATIONS']
2018-04-20 20:05:05,879: KeyError: 'SQLALCHEMY_TRACK_MODIFICATIONS'

How are you configuring SQLAlchemy to point to the database?

In models.py I'm defining a variable db = SQLAlchemy(). I then import that variable into __init__.py where i keep all my routes and functions, and finally initialise the database in a if __name__ == '__main__' statement, db.init_app(app). In that same if clause I'm also defining all the db connection stuff, app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://dellitsni:{password}@dellitsni.mysql.pythonanywhere-services.com/dellitsni${dbname}'

It looks like this:

init.py

# all functions and routes up here ^

if __name__ == '__main__':

    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://dellitsni:{password}@dellitsni.mysql.pythonanywhere-services.com/dellitsni${dbname}'

    app.secret_key = 'topsecret'

    db.init_app(app)

    # start the scheduler
    apsched = BackgroundScheduler()
    apsched.add_job(update_phases, 'interval', seconds=30)
    apsched.start()

    if 'liveconsole' not in gethostname():
        app.run()

models.py

# some imports

db = SQLAlchemy()

# all models down here v

That might be the problem -- the code inside your if __name__ == '__main__': isn't executed on PythonAnywhere, so you need to move the app.config stuff out of there. I should also say that the background scheduler should not be moved out -- it won't work inside PythonAnywhere, and would cause strange results if it did (as there will be multiple copies of your code running to handle the incoming traffic). The background stuff is best set up as a scheduled task on the "Tasks" page.

Embarassing, i should have figured that out long ago! Thanks for your help, the db is working as it's supposed to now.

Excellent, thanks for confirming!