sqlalchemy不承认价值
我有一个使用SQLalchemy与数据库接口的机器人。该机器人是使用Blask和Twilio创建的,并连接到AzuresQL数据库。保存响应时,我会使用代码:
db.save(dbTable(content=incoming_msg,
question=question,
user=User.query.filter(User.number == num).first()))
其中db.save
定义为
def save_and_commit(item):
db.session.add(item)
db.session.commit()
db.save = save_and_commit
DB表的设置如下:
class dbTable(db.Model):
__tablename__ = 'db_table'
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String, nullable=False)
question_id = db.Column(db.Integer, db.ForeignKey('dbTable_questions.id'))
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
def __init__(self, content, question, user):
self.content = content
self.question = question
self.user = user
这对某些DB表可以正常工作,但对其他表来说不行。对于某些表,我得到回应:
sqlalchemy.exc.IntegrityError: (pyodbc.IntegrityError) ('23000', "[23000] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Cannot insert the value NULL into column 'user_id', table 'test.dbo.baseline_answers'; column does not allow nulls. INSERT fails. (515) (SQLExecDirectW)")
我无法弄清楚为什么是这种情况。我尝试使用user.query.filter(user.number == num).first()
将用户对象创建一个用户对象,并将其作为参数传递给db.save
。尽管服务器确认用户并非没有,但一旦我尝试在DB中保存一些东西,我还是会得到上述响应。
真的很感谢您对这个问题的任何帮助。
I have a bot which uses sqlalchemy to interface with the database. The bot is created using Flask and Twilio and it is connected to an AzureSQL database. When saving a response, I use the code:
db.save(dbTable(content=incoming_msg,
question=question,
user=User.query.filter(User.number == num).first()))
where db.save
is defined as
def save_and_commit(item):
db.session.add(item)
db.session.commit()
db.save = save_and_commit
The db tables are set up as follows:
class dbTable(db.Model):
__tablename__ = 'db_table'
id = db.Column(db.Integer, primary_key=True)
content = db.Column(db.String, nullable=False)
question_id = db.Column(db.Integer, db.ForeignKey('dbTable_questions.id'))
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
def __init__(self, content, question, user):
self.content = content
self.question = question
self.user = user
This works fine for some db tables but not for others. For certain tables, I get the response:
sqlalchemy.exc.IntegrityError: (pyodbc.IntegrityError) ('23000', "[23000] [Microsoft][ODBC Driver 18 for SQL Server][SQL Server]Cannot insert the value NULL into column 'user_id', table 'test.dbo.baseline_answers'; column does not allow nulls. INSERT fails. (515) (SQLExecDirectW)")
I cannot figure out why this is the case. I have tried creating a user object with User.query.filter(User.number == num).first()
and passing that as as argument to db.save
. Despite the server confirming that the user is not none, as soon as I try to save something in the db I get the above response.
Would really appreciate any help on this problem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题在于定义用户对象的位置:尚未定义用户和数据库表之间的关系。
The problem was where the user object was defined: the relationship between the user and the db table hadn't been defined.