Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python “炼金术”;整数值不正确:';变量';对于列';id';第1行“;_Python_Sqlalchemy - Fatal编程技术网

Python “炼金术”;整数值不正确:';变量';对于列';id';第1行“;

Python “炼金术”;整数值不正确:';变量';对于列';id';第1行“;,python,sqlalchemy,Python,Sqlalchemy,每当我尝试在Flask的SQLAlchemy包装器上使用标准的“添加”功能时,都会出现以下错误: OperationalError: (_mysql_exceptions.OperationalError) (1366, "Incorrect integer value: 'username' for column 'id' at row 1") [SQL: u'INSERT INTO `Reflections` (id, username, entrydate, scale, activ

每当我尝试在Flask的SQLAlchemy包装器上使用标准的“添加”功能时,都会出现以下错误:

OperationalError: (_mysql_exceptions.OperationalError) 
(1366, "Incorrect integer value: 'username' for column 'id' at row 1") 

[SQL: u'INSERT INTO `Reflections` (id, username, entrydate, scale, activity, description) VALUES (%s, %s, %s, %s, %s, %s)'] 

[parameters: ('username', 'entrydate', 'scale', 'activity', 'description', None)]
这是我的models.py文件:

class Tasks(Base):
    __tablename__ = 'Tasks'
    id = Column(Integer, primary_key=True)
    list_id = Column(String(255), unique=False)
    username = Column(String(255), unique=False)
    title = Column(String(255), unique=False)
    description = Column(String(255), unique=False)
    created_at = Column(String(255), unique=False)
    starred = Column(String(255), unique=False)
    completed_on = Column(String(255), unique=False)


    def __init__(self, id=None, list_id=None, username=None, title=None, description=None, created_at=None, starred=None, completed_on=None):
        self.id = str(id)
        self.list_id = list_id
        self.username = username
        self.title = title
        self.description = description
        self.created_at = created_at
        self.starred = starred
        self.completed_on = completed_on


    def __repr__(self):
        return '<title %r>' % (self.title)  
出于某种原因,它试图在“id”列中插入自动递增的内容。有没有办法告诉SQLAlchemy忽略带有插入的列?

找到了答案! 这是因为
id
是在
\uuuu init\uuuu
方法中定义的。注释它/删除它,如下所示修复它:

class Tasks(Base):
    __tablename__ = 'Tasks'
    id = Column(Integer, primary_key=True)
    list_id = Column(String(255), unique=False)
    username = Column(String(255), unique=False)
    title = Column(String(255), unique=False)
    description = Column(String(255), unique=False)
    created_at = Column(String(255), unique=False)
    starred = Column(String(255), unique=False)
    completed_on = Column(String(255), unique=False)


    def __init__(self, id=None, list_id=None, username=None, title=None, description=None, created_at=None, starred=None, completed_on=None):
        # self.id = str(id)
        self.list_id = list_id
        self.username = username
        self.title = title
        self.description = description
        self.created_at = created_at
        self.starred = starred
        self.completed_on = completed_on


    def __repr__(self):
        return '<title %r>' % (self.title)
类任务(基本):
__tablename_uu='Tasks'
id=列(整数,主键=True)
list_id=列(字符串(255),unique=False)
username=列(字符串(255),unique=False)
title=列(字符串(255),unique=False)
description=列(字符串(255),unique=False)
在=列处创建(字符串(255),unique=False)
星号=列(字符串(255),唯一性=False)
已完成\u on=列(字符串(255),unique=False)
def uuu init uuuu(self,id=None,list_id=None,username=None,title=None,description=None,created_at=None,starred=None,completed_on=None):
#self.id=str(id)
self.list\u id=list\u id
self.username=用户名
self.title=标题
self.description=描述
self.created_at=created_at
self.starred=starred
self.completed\u on=已完成
定义报告(自我):
返回“”%(self.title)

我认为您缺少id参数。你可以这样做

i = Tasks(None, 'list_id', 'username', 'title', 'description', 'created_at', 'starred', 'completed_on')
db_session.add(i)
db_session.commit()
i = Tasks(None, 'list_id', 'username', 'title', 'description', 'created_at', 'starred', 'completed_on')
db_session.add(i)
db_session.commit()