Home > Enterprise >  cant commit() an updated data with sqlalchemy cant find whats wrong
cant commit() an updated data with sqlalchemy cant find whats wrong

Time:11-17

hello my sqlalchemy just wont commit im pretty sure that my code is correct but i have no idea what why it wont commit

class Movie(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(80), unique=True, nullable=False)
year = db.Column(db.String(80), nullable=False)
description = db.Column(db.String(180), nullable=False)
rating = db.Column(db.String(80), nullable=False)
ranking = db.Column(db.String(80), nullable=False)
review = db.Column(db.String(256), nullable=False)
img_url = db.Column(db.String(1000), nullable=False)

def __repr__(self):
    return f"id: {self.id} title: {self.title} year: {self.year} description: 
           {self.description} rating: {self.rating} " \
           f"ranking {self.ranking} review: {self.review} img_url: {self.img_url}"



@app.route('/edit', methods=['GET','POST'])
def edit_movie():
movie_id = request.args.get('id')
form = Edit_Form()
print(movie_id)

if request.method == 'POST':
    movie_update = Movie.query.filter_by(id=movie_id).first()
    movie_update.rating = form.rating.data
    movie_update.review = form.review.data
    db.session.commit()
    return redirect(url_for('home'))

return render_template('edit.html', form=form)

thats my html code

<form action="{{ url_for('edit_movie') }}" method="POST">
{{ form.csrf_token }}
<p>please put in your new rating</p>
{{ form.rating }}
<p>please write your review </p>
{{ form.review }}
{{ form.submit }}
</form>

it seems not to make any changes to the sql data

AttributeError: 'NoneType' object has no attribute 'rating'

CodePudding user response:

movie_update = Movie.query.filter_by(id=movie_id).first()

isn't finding a Movie, and is returning None.

CodePudding user response:

The "movie.update" object that you think you are creating, hasn't been created, whereby not only rating will fail as a method, but I am sur review, too. So, when that fails, nothing will commit

  • Related