I'm making a video page can track played time and comment time, every time I reload the page, using {{ movie.play_num }} to display the played time, it's always working but it won't change the database, So I don't know how can it be right? Seems like the session.commit() doesn't work.
Besides, if I put some words in the comment field and submit, a waiting for response... shows up and looks like the server keep waiting until timeout.
Here's my code:
def play(id=None):
movie = Movie.query.join(Tag).filter(
Tag.id == Movie.tag_id,
Movie.id == int(id)
).first_or_404()
form = CommentForm()
if "user" in session and form.validate_on_submit():
data = form.data
comment = Comment(
content=data["content"],
movie_id=movie.id,
user_id=session["user_id"]
)
db.session.add(comment)
db.session.commit() # problem here
movie.comment_num = movie.comment_num + 1
flash("Success", "ok")
return redirect(url_for("home.play", id=movie.id))
movie.play_num = movie.play_num + 1
try:
db.session.commit() # and problem here
except:
db.session.rollback()
return render_template("home/play.html", movie=movie, form=form)
see the play_num has changed to 1 from 0, this is the first time I reload the page, but at the second time, the page can't be open, and the console can't collect any data.An error occurred:
This Session's transaction has been rolled back due to a previous exception during flush. To begin a new transaction with this Session, first issue Session.rollback(). Original exception was: (raised as a result of Query-invoked autoflush; consider using a session.no_autoflush block if this flush is occurring prematurely) (pymysql.err.InternalError)
How to fix this

joinin your query ?