gpt4 book ai didi

python - 属性错误 : 'NoneType' object has no attribute 'time_recorded' in Flask, SQLAlchemy

转载 作者:行者123 更新时间:2023-12-01 09:17:48 25 4
gpt4 key购买 nike

我有一个 API 端点,它传递一个用于在数据库中进行调用的变量。由于某种原因,它无法运行查询,但语法是正确的。我的代码如下。

@app.route('/api/update/<lastqnid>')
def check_new_entries(lastqnid):
result = Trades.query.filter_by(id=lastqnid).first()
new_entries = Trades.query.filter(Trades.time_recorded > result.time_recorded).all()

id 字段是:

id = db.Column(db.String,default=lambda: str(uuid4().hex), primary_key=True)

我尝试了 filter 而不是 filter_by,但它不起作用。当我删除 filter_by(id=lastqnid) 时,它起作用了。它没有运行查询的原因可能是什么?

查询的交易表是

class Trades(db.Model):
id = db.Column(db.String,default=lambda: str(uuid4().hex), primary_key=True)
amount = db.Column(db.Integer, unique=False)
time_recorded = db.Column(db.DateTime, unique=False)

最佳答案

您遇到的问题似乎是在使用结果之前没有检查是否发现任何内容

@app.route('/api/update/<lastqnid>')
def check_new_entries(lastqnid):
result = Trades.query.filter_by(id=lastqnid).first()
# Here result may very well be None, so we can make an escape here
if result == None:
# You may not want to do exactly this, but this is an example
print("No Trades found with id=%s" % lastqnid)
return redirect(request.referrer)
new_entries = Trades.query.filter(Trades.time_recorded > result.time_recorded).all()

关于python - 属性错误 : 'NoneType' object has no attribute 'time_recorded' in Flask, SQLAlchemy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51077280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com