sqlalchemy.exc.IntegrityError:(sqlite3.IntegrityError)NOT NULL约束失败:student.strength

时间:2020-05-12 07:24:00

标签: python sql python-3.x sqlalchemy flask-sqlalchemy

我正在构建我创建的基本烧瓶应用程序和html表单,以通过文本框接受数据

    @app.route('/add', methods=['GET','POST'])
def add_class():
    if request.method == "POST":

        size = request.form["size"]
        Name = request.form["class"]
        data = student(strength=size , name= Name)   
        db.session.add(data)
        db.session.commit()   

        return redirect('/view')             
    else : 
        return render_template("attend.html")

然后我使用request.form []将数据打包到我的烧瓶应用程序中 而且我还手动在变量中输入了一个值进行检查,但仍然无法正常工作。


app = Flask(__name__)
app.secret_key="appu"
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///attend.sqlite3'
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False

db = SQLAlchemy(app)


class student(db.Model):
    class_id = db.Column(db.Integer , primary_key = True)
    strength = db.Column(db.String , nullable = False )
    name = db.Column(db.String , nullable = False)

    def __init__(self ,strength ,name , **kwargs):
        super(student, self).__init__(**kwargs)
        self.strength
        self.name 

但是它返回了以上错误,我还检查了request.form是否通过返回变量来获取任何数据。

->这是我的模型课

class Polynomial:
    def __init__(self, *coefficients):
        self.coefficients = coefficients
    def __len__(self):
        return len(self.coefficients)
    def __repr__(self):
        equation = "("
        for i in range(len(self)):
            if i != 0:
                equation += "+"
            equation += str(self.coefficients[i]) + "x^" + str(i)
        return equation + ")"
    def __lt__(self, other):
        return self.coefficients < other.coefficients
    def __ge__(self, other):
        return self.coefficients >= other.coefficients 
a = Polynomial(1,2,3)
print(a)

,我还检查了是否覆盖了init并应用了修复程序。 您可以在上面的init中看到** kwargs语句。

我每次都收到相同的错误报告。

非常需要任何帮助。

0 个答案:

没有答案