sqlalchemy.exc.IntegrityError:(sqlite3.IntegrityError)NOT NULL 约束失败:movie. rating

问题描述 投票:0回答:1

美好的一天 我在我的 python 程序上使用 SQLAlchemy 在 SQLite 3 上创建了下表。 电影类(db.Model):

id: Mapped[int] = mapped_column(Integer, primary_key=True)
title: Mapped[str] = mapped_column(String(250), unique=True, nullable=False)
year: Mapped[int] = mapped_column(Integer, nullable=False)
description: Mapped[str] = mapped_column(String(250), nullable=False)
rating: Mapped[float] = mapped_column(Float, nullable=True)
ranking: Mapped[int] = mapped_column(Integer, nullable=True, autoincrement=True)
review: Mapped[str] = mapped_column(String(250), nullable=True)
img_url: Mapped[str] = mapped_column(String(250), nullable=False)

我试图在我的 SQLite 表上添加一个新行,其中包含通过执行以下代码从 url 检索到的信息:(请注意,提供的唯一信息是针对我的 SQLite 数据库列上可空设置为 False 的列)

@app.route(“/查找”) def find_movie():

movie_api_id = request.args.get("id")
if movie_api_id:
    movie_api_url = f"{MOVIE_DB_INFO_URL}/{movie_api_id}"
    response = requests.get(movie_api_url, params={"api_key": MOVIES_API_KEYS, "language": "en
       US"})
    data = response.json()
    new_movie = Movie(
        title=data["title"],
        year=data["release_date"].split("-")[0],
        img_url=f"https://image.tmdb.org/t/p/w500{data['poster_path']}",
        description=data["overview"]
    )
    db.session.add(new_movie)
    db.session.commit()

    return redirect(url_for("rate_movie", id=new_movie.id))

当我运行代码时,我收到下面的错误消息

sqlalchemy.exc.IntegrityError:(sqlite3.IntegrityError)NOT NULL 约束失败:movie. rating [SQL:插入电影(标题、年份、描述、评级、排名、评论、img_url)值(?、?、?、?、?、?、?)] [参数:('角斗士', '2000', “180年,皇帝马库斯·奥勒留的去世使罗马帝国陷入混乱。马克西姆斯是罗马军队中最有能力和最信任的人之一......(168个字符被截断) ...电子交易员。改名为西班牙人并被迫成为角斗士,马克西姆斯必须与其他人战斗至死,以娱乐付费观众。”,无,无,无,'https://image.tmdb.org/ t/p/w500/ty8TGRuvJLPUMAR1H1nRIsgwvim.jpg')] (此错误的背景位于:https://sqlalche.me/e/20/gkpj

python sqlalchemy flask-sqlalchemy
1个回答
-1
投票

您可能需要从头开始重新创建表格。 您可能在将此

nullable
设置为 True 之前创建了它。

如果它不起作用,您可以在此处显示为创建表而执行的 sql 代码。

© www.soinside.com 2019 - 2024. All rights reserved.