[英]How to fix “peewee.IntegrityError: NOT NULL constraint failed: fiskalna.user_id”
I am trying to make my first web app with Flask but when I run it I get an error: peewee.IntegrityError: NOT NULL constraint failed: fiskalna.user_id 我正在尝试使用Flask制作我的第一个Web应用程序,但是当我运行它时出现错误:peewee.IntegrityError:NOT NULL约束失败:fiskalna.user_id
Can someone help me? 有人能帮我吗?
I have searched everywhere even here there is a similar question but it did't solved my problem. 我搜索了所有地方,即使在这里也有类似的问题,但并没有解决我的问题。 If there is a problem regarding my question, please excuse me I am still beginner. 如果我的问题有问题,请原谅我仍然是初学者。
models.py models.py
raise ValueError("User exsists")
class Fiskalna(Model):
user=ForeignKeyField(
model=User,
backref='fiskalna')
br_fiskalna=IntegerField(unique=True)
suma=IntegerField()
dnaok=FloatField()
dodanena_na=DateTimeField(default=datetime.datetime.now)
class Meta():
database=DATABASE
order_by=("-dodanena_na")
app.py app.py
@app.route("/new", methods=["GET", "POST"])
@login_required
def post():
form=forms.FiskalnaForm()
if form.validate_on_submit():
models.Fiskalna.create(
br_fiskalna=form.br_fiskalna.data,
suma=form.suma.data,
danok=form.danok.data)
flash("Postiranoo", "success")
return redirect(url_for("register"))
return render_template("post.html", form=form)
I don't know too much about Peewee, but I can see you have declared a ForeignKeyField
"User", which you haven't populated in your .create(...
call. 我对Peewee不太了解,但是我可以看到您已声明一个ForeignKeyField
“ User”,您没有在.create(...
调用中填充它。
I'm assuming in the database, the ForeignKeyField results in a NOT NULL column being created on the fiskalna
table - user_id
, which is what is giving the IntegrityError. 我假设在数据库中,在NOT NULL列ForeignKeyField结果被上创建fiskalna
表- user_id
,这是什么给IntegrityError。
To solve, when you want to add a new Fiskalna
record, you'll need to include user=your_user_record
in the create()
call. 为了解决此问题,当您要添加新的Fiskalna
记录时,需要在create()
调用中包括user=your_user_record
。
Sidenote, you also have a typo in create on the danok
parameter - it doesn't match the model column. 旁注,您还需要在danok
参数上创建一个错字-它与模型列不匹配。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.