Flask-SQLAlchemy InvalidRequestError: Object is already attached to session -


i'm creating forum project using flask, , managing users, threads, posts, etc. using flask-sqlalchemy. however, i've found when attempt x (e.g. edit post), invalidrequesterror if attempt else (e.g. delete post).

for editing post,

def post_edit(id, t_id, p_id):   post = post.query.filter_by(id=p_id).first()   if post.author.username == g.user.username:     form = postform(body=post.body)     if form.validate_on_submit():       post.body = form.body.data       db.session.commit()       return redirect(url_for('thread', id=id, t_id=t_id))     return render_template('post_edit.html', form=form, title='edit')   else:     flash('access denied.')     return redirect(url_for('thread', id=id, t_id=t_id)) 

and deleting post,

@app.route('/forum=<id>/thr=<t_id>/p=<p_id>/delete', methods=['get','post']) def post_delete(id, t_id, p_id):   post = post.query.filter_by(id=p_id).first()   if post.author.username == g.user.username:     db.session.delete(post)     db.session.commit()     return redirect(url_for('thread', id=id, t_id=t_id))   else:     flash('access denied.')     return redirect(url_for('thread', id=id, t_id=t_id)) 

and posting post

@app.route('/forum/id=<id>/thr=<t_id>', methods=['get','post']) def thread(id, t_id):   forum = forum.query.filter_by(id=id).first()   thread = thread.query.filter_by(id=t_id).first()   posts = post.query.filter_by(thread=thread).all()   form = postform()   if form.validate_on_submit():     post = post(body=form.body.data,                 timestamp=datetime.utcnow(),                 thread=thread,                 author=g.user)     db.session.add(post)     db.session.commit()     return redirect(url_for('thread', id=id, t_id=t_id))   return render_template('thread.html', forum=forum, thread=thread, posts=posts, form=form, title=thread.title) 

unfortunately, surefire way make problem resolve reset script runs app, run.py

#!bin/python  app import app app.run(debug=true,host='0.0.0.0') 

are using wooshalchemy because might part of problem. described here

he describes "fix" requires modification of wooshalchemy extension.

usually though mean called post model object , attached using "session.add" , tried "session.delete" or did "session.add" on same object.

also request routing bit strange flask i've never seen "thr=<t_id>" type of notation flask before. has been working you?

http://flask.pocoo.org/docs/quickstart/#variable-rules


Comments

  1. Your blog is in a convincing manner, thanks for sharing such an information with lots of your effort and time
    sql server dba online training
    sql database administrator training

    ReplyDelete

Post a Comment

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -