首页 > 解决方案 > 烧瓶中的 sqlalchemy.exc.OperationalError

问题描述

您好,我是一名新程序员,我刚开始用烧瓶制作网站,在我最近的项目中,我遇到了数据库问题(错误名称是标题;))这是我的代码(我正在制作一个迷你 youtube)

@app.route("/create_acc", methods=["POST", "GET"])
def create_acc():
    if request.method == "POST":
        name = request.form["nm"]

        check = ChannelDB.query.filter_by(name=name).first()

    if not check:
        email = request.form["em"]
        psw = request.form["ps"]

        channel = ChannelDB(name, email, psw, 0, 0)
        db.session.add(channel)
        db.session.commit()

        return redirect(url_for("user"))
    else:
        abort(409, message="Video alredy exists")

这是产生错误的地方。

class ChannelDB(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), nullable=False)
email = db.Column(db.String(100), nullable=False)
password = db.Column(db.String(100), nullable=False)
subs = db.Column(db.Integer, nullable=False)
num_of_videos = db.Column(db.Integer, nullable=False)

这是我的数据库。

{% extends "base.html" %}
{% block title %}Login Page{% endblock %}
{% block content %}
    <form action="/create_acc", method="post">
        <p>Name: </p>
        <p><input type="text" name="nm"></p>
        <p>Email: </p>
        <p><input type="text" name="em"></p>
        <p>Password: </p>
        <p><input type="text" name="ps"></p>
        <p>Press this button when you fill the spots above</p>
        <p><input type="submit" value="submit"></p>
    </form>
{% end block %}

这是我的登录名。.html 文件

标签: flaskflask-sqlalchemy

解决方案


错误的完整摘录会更具决定性。不过,如果您能够使用终端在表中添加记录,请尝试。

from app import *
channel = ChannelDB("Sample name", "sample email", "pass", 0, 0)
db.session.add(channel)
db.commit()

如果上述命令未执行,请删除表并重新创建它们。

from app import db
db.drop_all()
db.create_all()

如果没有,请尝试重新创建表


推荐阅读