sqlalchemy不承认价值

发布于 2025-02-10 06:13:47 字数 1476 浏览 3 评论 0原文

我有一个使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

默嘫て 2025-02-17 06:13:47

问题在于定义用户对象的位置:尚未定义用户和数据库表之间的关系。

The problem was where the user object was defined: the relationship between the user and the db table hadn't been defined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文