python - SQLAlchemy session error: InvalidRequestError -


i trying commit query results @ same time db , ended error.

sqlalchemy.exc.invalidrequesterror: session in 'committed' state; no further sql can emitted within transaction. 

full traceback:

traceback (most recent call last):   file "c:\python34\lib\site-packages\flask\app.py", line 1836, in __call__     return self.wsgi_app(environ, start_response)   file "c:\python34\lib\site-packages\flask\app.py", line 1820, in wsgi_app     response = self.make_response(self.handle_exception(e))   file "c:\python34\lib\site-packages\flask\app.py", line 1403, in handle_exception     reraise(exc_type, exc_value, tb)   file "c:\python34\lib\site-packages\flask\_compat.py", line 33, in reraise     raise value   file "c:\python34\lib\site-packages\flask\app.py", line 1817, in wsgi_app     response = self.full_dispatch_request()   file "c:\python34\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request     rv = self.handle_user_exception(e)   file "c:\python34\lib\site-packages\flask\app.py", line 1381, in handle_user_exception     reraise(exc_type, exc_value, tb)   file "c:\python34\lib\site-packages\flask\_compat.py", line 33, in reraise     raise value   file "c:\python34\lib\site-packages\flask\app.py", line 1473, in full_dispatch_request     rv = self.preprocess_request()   file "c:\python34\lib\site-packages\flask\app.py", line 1666, in preprocess_request     rv = func()   file "c:\users\ajay\pycharmprojects\pypix-flask\codehackr\auth\views.py", line 14, in before_request     if current_user.is_authenticated():   file "c:\python34\lib\site-packages\werkzeug\local.py", line 338, in __getattr__     return getattr(self._get_current_object(), name)   file "c:\python34\lib\site-packages\werkzeug\local.py", line 297, in _get_current_object     return self.__local()   file "c:\python34\lib\site-packages\flask_login.py", line 46, in <lambda>     current_user = localproxy(lambda: _get_user())   file "c:\python34\lib\site-packages\flask_login.py", line 794, in _get_user     current_app.login_manager._load_user()   file "c:\python34\lib\site-packages\flask_login.py", line 363, in _load_user     return self.reload_user()   file "c:\python34\lib\site-packages\flask_login.py", line 325, in reload_user     user = self.user_callback(user_id)   file "c:\users\ajay\pycharmprojects\pypix-flask\codehackr\models.py", line 296, in load_user     u = db.session.query(user, unread_count).filter(user.id == user_id).first()   file "c:\python34\lib\site-packages\sqlalchemy\orm\query.py", line 2333, in first     ret = list(self[0:1])   file "c:\python34\lib\site-packages\sqlalchemy\orm\query.py", line 2200, in __getitem__     return list(res)   file "c:\python34\lib\site-packages\sqlalchemy\orm\query.py", line 2404, in __iter__     return self._execute_and_instances(context)   file "c:\python34\lib\site-packages\sqlalchemy\orm\query.py", line 2417, in _execute_and_instances     close_with_result=true)   file "c:\python34\lib\site-packages\sqlalchemy\orm\query.py", line 2408, in _connection_from_session     **kw)   file "c:\python34\lib\site-packages\sqlalchemy\orm\session.py", line 843, in connection     close_with_result=close_with_result)   file "c:\python34\lib\site-packages\sqlalchemy\orm\session.py", line 847, in _connection_for_bind     return self.transaction._connection_for_bind(engine)   file "c:\python34\lib\site-packages\sqlalchemy\orm\session.py", line 298, in _connection_for_bind     self._assert_active()   file "c:\python34\lib\site-packages\sqlalchemy\orm\session.py", line 192, in _assert_active     "this session in 'committed' state; no further " sqlalchemy.exc.invalidrequesterror: session in 'committed' state; no further sql can emitted within transaction. 

views.py

@main.route('/user/<username>', methods=['get','post']) def user(username):     form = skillform()     if form.validate_on_submit():         skill_add = skill(skill=form.skill.data, author=current_user._get_current_object())         db.session.add(skill_add)         return redirect(url_for('main.index'))     user = user.query.filter_by(username=username).first_or_404()     page = request.args.get('page', 1, type=int)     pagination = user.posts.order_by(post.timestamp.desc()).paginate(         page, per_page=current_app.config['posts_per_page'],         error_out=false)     posts = pagination.items     skills = user.skills.all()     return render_template('user/user.html', user=user, posts=posts,                            pagination=pagination, form=form, skills=skills) 

how solve error. thanks

since have added objects session via db.session.add(), must first commit changes, or roll them back, in order able make queries using same session.

db.session.add(skill_add) db.session.commit() # add line return redirect(url_for('main.index')) 

adding line specified result in code working expect.

also, although you're adding skill, querying user, error raised because 2 bound relationship, when query user query skills user has.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -