每当我尝试在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)
我的添加功能:
i = Tasks('list_id', 'username', 'title', 'description', 'created_at', 'starred', 'completed_on')
db_session.add(i)
db_session.commit()
由于某种原因,它试图插入'id'列,这是自动增量。有没有办法告诉SQLAlchemy忽略带插入的列?
答案 0 :(得分:0)
想出来!
这是因为id
是在__init__
方法中定义的。如下所述将其删除/删除修复它:
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)
答案 1 :(得分:0)
我相信您缺少id参数。您可以这样做。
i = Tasks(None, 'list_id', 'username', 'title', 'description', 'created_at', 'starred', 'completed_on')
db_session.add(i)
db_session.commit()