postgresql - DataError: (DataError) invalid input syntax for integer: "None" -
background
we're using flask develop simple application "scratch itch" realized had while working on previous project. session management handled through flask-login extension, coupled github-flask extension use user authentication. app uses flask-sqlalchemy , psycopg2 (2.6.3) connect postgresql 9.1 database user data stored.
issue
when app run locally (or deployed remote server) testing we're seeing strange condition during first login attempt. first time log in through github authentication sets, see error.
dataerror: (dataerror) invalid input syntax integer: "none" line 3: "user".id = 'none' ^ 'select "user".id user_id, "user".date_created user_date_created, "user".date_modified user_date_modified, "user".nickname user_nickname, "user".email user_email, "user".about_me user_about_me, "user".github_access_token user_github_access_token, "user".github_id user_github_id \nfrom "user" \nwhere "user".id = %(param_1)s' {'param_1': u'none'}
i can close browser tab , revisit application url , see same message. however, if close browser window, clear browser's cache, , attempt log in, works expected , i'm able use application. point on can log out, switch browsers, clear browser cache again, etc., , there don't appear problems -- it's first login until browser cache cleared happens.
my testing shows happens on user , 2 different users can in 2 different states @ same time (one has cleared cache , isn't having problems, other hasn't taken steps still stuck in error state.
originally used local sqlite database instead of postgres. issue not exist there. has occurred since switch postgres.
i'm @ loss steps can take remedy situation. far questions know ask haven't been able lead right answers when search online.
for reference, i'll include full stack trace well.
full stack trace
traceback (most recent call last): file "/users/dev/product/venv/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__ return self.wsgi_app(environ, start_response) file "/users/dev/product/venv/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app response = self.make_response(self.handle_exception(e)) file "/users/dev/product/venv/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception reraise(exc_type, exc_value, tb) file "/users/dev/product/venv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app response = self.full_dispatch_request() file "/users/dev/product/venv/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request rv = self.handle_user_exception(e) file "/users/dev/product/venv/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception reraise(exc_type, exc_value, tb) file "/users/dev/product/venv/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request rv = self.dispatch_request() file "/users/dev/product/venv/lib/python2.7/site-packages/flask_debugtoolbar/__init__.py", line 124, in dispatch_request return view_func(**req.view_args) file "/users/dev/product/app/modules/mod_profile/controllers.py", line 26, in profile title='product') file "/users/dev/product/venv/lib/python2.7/site-packages/flask/templating.py", line 126, in render_template ctx.app.update_template_context(context) file "/users/dev/product/venv/lib/python2.7/site-packages/flask/app.py", line 716, in update_template_context context.update(func()) file "/users/dev/product/venv/lib/python2.7/site-packages/flask_login.py", line 825, in _user_context_processor return dict(current_user=_get_user()) file "/users/dev/product/venv/lib/python2.7/site-packages/flask_login.py", line 794, in _get_user current_app.login_manager._load_user() file "/users/dev/product/venv/lib/python2.7/site-packages/flask_login.py", line 363, in _load_user return self.reload_user() file "/users/dev/product/venv/lib/python2.7/site-packages/flask_login.py", line 325, in reload_user user = self.user_callback(user_id) file "/users/dev/product/app/modules/mod_auth/controllers.py", line 31, in load_user return user.query.get(id) file "/users/dev/product/venv/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 840, in return loading.load_on_ident(self, key) file "/users/dev/product/venv/lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 231, in load_on_ident return q.one() file "/users/dev/product/venv/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2395, in 1 ret = list(self) file "/users/dev/product/venv/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2438, in __iter__ return self._execute_and_instances(context) file "/users/dev/product/venv/lib/python2.7/site-packages/sqlalchemy/orm/query.py", line 2453, in _execute_and_instances result = conn.execute(querycontext.statement, self._params) file "/users/dev/product/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 729, in execute return meth(self, multiparams, params) file "/users/dev/product/venv/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 322, in _execute_on_connection return connection._execute_clauseelement(self, multiparams, params) file "/users/dev/product/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 826, in _execute_clauseelement compiled_sql, distilled_params file "/users/dev/product/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 958, in _execute_context context) file "/users/dev/product/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1159, in _handle_dbapi_exception exc_info file "/users/dev/product/venv/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 199, in raise_from_cause reraise(type(exception), exception, tb=exc_tb) file "/users/dev/product/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 951, in _execute_context context) file "/users/dev/product/venv/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 436, in do_execute cursor.execute(statement, parameters) dataerror: (dataerror) invalid input syntax integer: "none" line 3: "user".id = 'none' ^ 'select "user".id user_id, "user".date_created user_date_created, "user".date_modified user_date_modified, "user".nickname user_nickname, "user".email user_email, "user".about_me user_about_me, "user".github_access_token user_github_access_token, "user".github_id user_github_id \nfrom "user" \nwhere "user".id = %(param_1)s' {'param_1': u'none'}
resolved!
this race condition. during login process our application redirects authenticated user profile page. getting triggered correctly, new users getting called before user committed database.
visually happened @ same time, confirm callback supplied "right" information, wasn't until deconstructed , reconstructed login steps realized hitting route before record committed database. since function added record wasn't interrupted redirect did supposed do, hair late.
perhaps else find extract of code helpful when troubleshooting similar issue:
## mod_auth/controllers.py @mod_auth.before_app_request def before_request(): g.user = current_user @mod_auth.route('/login', methods=['get']) def login(): if g.user.is_authenticated(): redirect(url_for('mod_home.index')) return github.authorize() @login_manager.user_loader def load_user(id): return user.query.get(id) @github.access_token_getter def token_getter(): user = g.user if user not none: return user.github_access_token @mod_auth.route('/github') @github.authorized_handler def authorized(oauth_token): next_url = request.args.get('next') or url_for('mod_home.index') if oauth_token none: flash("authorization failed.") return redirect(next_url) user = user.query.filter_by(github_access_token=oauth_token).first() if user none: user = user(github_access_token=oauth_token) db.session.add(user) db.session.commit() ## <-- adding commit here fixed issue user.github_access_token = oauth_token login_user(user) ## login_user called, because oauth_token exist , had been ## added session, user flagged "is_authenticated" ## , redirect triggered ghinfo = github.get('user') if 'login' in ghinfo: user.nickname = ghinfo['login'].lower() else: user.nickname = 'nameless' db.session.commit() ## commit, return redirect(url_for('mod_profile.profile', username=user.nickname))
Comments
Post a Comment