So, I was trying to create a Movie class object but I kept getting IntegrityError despite the fact that I've set nullable=True for ranking attribute in the following class definition.
class Movie(db.Model):
id = db.Column(db.Integer, primary_key=True)
ranking = db.Column(db.Integer, nullable=True)
title = db.Column(db.String(250), unique=True, nullable=False)
year = db.Column(db.Integer, nullable=False)
description = db.Column(db.String(500), nullable=False)
rating = db.Column(db.Float, nullable=False)
review = db.Column(db.String(250), nullable=False)
img_url = db.Column(db.String, nullable=False)
@app.route("/")
def home():
all_movies = Movie.query.order_by(Movie.rating).all()
for i in range(len(all_movies)):
all_movies[i].ranking = len(all_movies) - i
db.session.commit()
return render_template("index.html", movies=all_movies)
@app.route('/add', methods=['GET', 'POST'])
def add():
form = AddMovieForm()
if form.validate_on_submit():
new_movie = Movie(title=form.title.data, year=form.year.data, description=form.description.data, img_url=form.img_url.data, rating=form.rating.data, review=form.review.data)
db.session.add(new_movie)
db.session.commit()
return redirect(url_for('.home'))
return render_template('add.html', form=form)
Error:
IntegrityError sqlalchemy.exc.IntegrityError: (sqlite3.IntegrityError)
NOT NULL constraint failed: movie.ranking [SQL: INSERT INTO movie
(ranking, title, year, description, rating, review, img_url) VALUES
(?, ?, ?, ?, ?, ?, ?)] [parameters: (None, 'Pride & Prejudice', 2005,
'Sparks fly when spirited Elizabeth Bennet meets single, rich, and
proud Mr. Darcy. But Mr. Darcy reluctantly finds himself falling in
love with a woman beneath his class. Can each overcome their own pride
and prejudice?', 9.3, 'Could I be any more in love with this movie?',
'https://www.google.com/imgres?imgurl=https%3A%2F%2Fs3.amazonaws.com%2Fstatic.rogerebert.com%2Fuploads%2Fmovie%2Fmovie_poster%2Fpride-and-prejudice-20
... (189 characters truncated) ...
IARDZAQ..i&docid=wCdoo2CscAy5JM&w=400&h=600&q=pride%20and%20prejudice%20movie%20description&hl=en&ved=2ahUKEwi1oaaXuu_3AhUgx6ACHcyfBSAQMygBegUIARDZAQ')]
(Background on this error at: https://sqlalche.me/e/14/gkpj)