python - Flask SQLAlchemy Many to Many Delete Element -


my flask site has suggestions , users.

class suggestion(db.model):     id = db.column(db.integer, primary_key=true)     user_nickname = db.column(db.integer, db.foreignkey('user.nickname'))     voters = db.relationship('user', secondary=votes, lazy='dynamic')  class user(db.model):     id = db.column(db.integer, primary_key = true)     suggestions = db.relationship('suggestion', backref='user', lazy='dynamic')     votes = db.relationship('suggestion', secondary=votes, lazy='dynamic')  

i have set many many relationship between 2 such.

votes = db.table('votes',     db.column('user_id', db.integer, db.foreignkey('user.id')),     db.column('suggestion_id', db.integer, db.foreignkey('suggestion.id')) ) 

when try delete suggestion inside views.py error.

the view:

@app.route('/_delete_suggestion', methods=['get', 'post'])     def delete_suggestion():     user_nickname = request.form.get('user_nickname')     user = user.query.filter_by(nickname=user_nickname).first()     sugg_id = request.form.get('sugg_id')     sugg = suggestion.query.filter_by(id=sugg_id).first()     if user.is_special:         db.session.delete(sugg)         db.session.commit()     else:         flash('you not cool enough that!')         return redirect(url_for('index'))     return  @app.route('/_upvote', methods=['get', 'post'])     def upvote():     user_nickname = request.form.get('user_nickname')     user = user.query.filter_by(nickname=user_nickname).first()     sugg_id = request.form.get('sugg_id')     sugg = suggestion.query.filter_by(id=sugg_id).first()     if sugg.voters.filter_by(nickname=user_nickname).first() not none:         print("err", file=sys.stderr)     else:         print("voted", file=sys.stderr)         sugg.score += 1         sugg.voters.append(user)         user.votes.append(sugg)         db.session.add(sugg)         db.session.add(user)         db.session.commit()     return 

the error:

staledataerror: delete statement on table 'votes' expected delete 2 row(s); 4 matched. 

what know:

  • i can delete suggestions don't have votes.

what have tried:

  • i tried adding view-only=true models.
  • i tried removing suggestion's voters , removing suggestion users votes before deleting.
  • i have been looking @ posts form stackoverflow , google related issue without luck.

what missing? driving me wall!

edit: inside python interpreter, tried following.

s = suggestion.query.first() v in s.voters.all():     s.voters.remove(v) s.voters.all() 

this crashed , produced same staledataerror.

i figured out! adding many many array twice inside _upvote route.

original _upvote snippet:

print("voted", file=sys.stderr) sugg.score += 1 sugg.voters.append(user) user.votes.append(sugg) db.session.add(sugg) db.session.add(user) db.session.commit() 

fixed _upvote snippet:

print("voted", file=sys.stderr) sugg.score += 1 sugg.voters.append(user) db.session.add(sugg) db.session.commit() 

Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

java.lang.NoClassDefFoundError When Creating New Android Project -

Decoding a Python 2 `tempfile` with python-future -