0

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)

enter image description here

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

5
  • Is your database connection setup correctly? What's the result if inserting a comment in python console? Commented Mar 14, 2019 at 2:15
  • Connection is correct, inserting a row works okay Commented Mar 14, 2019 at 2:45
  • How about putting a breakpoint after the commit to verify if it really the commit problem Commented Mar 14, 2019 at 3:30
  • 1
    Can you really update data like that when you perform a join in your query ? Commented Mar 14, 2019 at 10:07
  • It is not possible to answer this properly without seeing the error tracebacks Commented May 16 at 17:32

1 Answer 1

1

you try catch the exception msg:

from sqlalchemy.exc import SQLAlchemyError
try:
    db.session.commit()
except SQLAlchemyError as e:
    print(str(e))
    db.session.rollback()

check what the error msg first

Sign up to request clarification or add additional context in comments.

1 Comment

It didn't go through the except way, it did choose to commit but the data never changed in database

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.