python - Flask __init__() 缺少 1 个必需的位置参数 : 'id'

标签 python sqlalchemy

这可能是一个菜鸟问题,但我总是无缘无故地收到此错误

__init __() 缺少 1 个必需的位置参数:'id'

class test1(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True) # <-- it's right there 
idNo = db.Column(db.Integer,unique=True)
Name = db.Column(db.String(255))
Type = db.Column(db.String(255))
Date = db.Column(db.String(255))
style = db.Column(db.String(255))
from = db.Column(db.String(255))
pCode = db.Column(db.String(25),unique=True)



def __init__(self,id,idNo,Name,Type,Date,style,from,pCode):
    self.id = id  # <-- it's right there 
    self.idNo= idNo
    self.Name = Name
    self.Type = Type
    self.Date = Date
    self.style = style
    self.from = history
    self.pCode = pCode

 jD = {idNo:"Integer_value",Name:"value",Type:"value",Date:"value",style:"value",from:"value",pCode:"value"}
for j in jD:

         x = test1(
         idNo=j["idNo"],
        Name=j["Name"],
        Type=j["Type"],
        Date=j["Date"],
        style=j["style"],
        from =j["from"],
        pCode=pCode)

        db.session.add(x)

我在 Python 上不断收到此错误 >> __init__() 缺少 1 个必需的位置参数:'id' 我是否应该在创建新类(class)时提及“整数自动增量”? 或者我是否必须从 class test1()__init __() 中删除它?

最佳答案

要么在创建 test1 对象时传递 id 值,要么从 init 函数中删除“id”参数,因为 init 在创建对象时需要 id。 “id”的自动递增可以由数据库处理。所以你不需要使用代码设置“id”的值。

关于python - Flask __init__() 缺少 1 个必需的位置参数 : 'id' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53546640/

相关文章:

python - 错误 : command 'gcc' failed with exit status - while installing ibm_db on docker centos

python - SQLAlchemy 返回元组而不是字典

python - 在opencv Python中拖动鼠标画线并获取线的端点坐标

python - 如何在 wxPython 中管理动态、变化的主菜单?

python - 将 Array_list 值插入另一个空 Array_list

python - 如何使用 conda 在 GoogleColab 中永久安装包?

python - 在 sqlalchemy 中,如何获取与表中所有行匹配的查询,以便另一个表中没有行链接到它?

python - 使用多个外键

python - 无法将对象从一个数据库移动到另一个数据库

python - 相关子项更改时 SQLAlchemy 更新父项