Python Flask-SQLAlchemy 使用疑惑

2015-12-30 08:47:50 +08:00
 nersle

使用 Flask-SQLAlchemy 时遇到一个很奇怪的问题,使用下面的代码插入数据库会成功:
report = Report(tid=vulId, vulURL=vulURL, homeTitle=homeTitle, homeLink=homeLink, DBInfo=DBInfo,vulId=vulParId, UserLists=UserLists, dbsLists=dbsLists, content=content)
db.session.add(report)

奇怪的是 Mysql 中确实插入成功了,程序会跳转到 /report/view/vulId
return redirect("/report/view/"+str(vulId))

在 viewReport 函数中使用如下代码查询后返回 None :
report = Report.query.filter(Report.tid==vulId).first()

,但是此时如果在 model.py 的 main 函数中执行
report = Report.query.filter(Report.tid==vulId).first()

却会返回正确数据,此时再在 viewReport 函数中执行就不为空了。多次测试发现失败概率在 30%左右,现在不知道该怎么定位错误,麻烦各位指教!

相关代码

@app.route("/report/view/<int:vulId>", methods=["GET"])
def viewReport(vulId=-1):
if vulId < 0:
return redirect("/vul/list")

title = "Report-{}".format(vulId)
report = Report.query.filter(Report.tid==vulId).first()

if report == None:  
    reUrl = request.headers.get("Referer")
    return redirect(reUrl)  

return render_template("report.html", title=title, report=report)

@app.route("/report/make/<int:vulId>", methods=["GET", "POST"])
def makeReport(vulId=-1):
if vulId < 0:
return redirect("/vul/list")

form = ReportForm()
# print form.errors
if form.validate_on_submit():
    # tid = form.tid.data
    vulURL = form.vulURL.data
    homeTitle = form.homeTitle.data
    homeLink = form.homeLink.data
    DBInfo = form.DBInfo.data
    vulParId = form.vulId.data
    UserLists = form.UserLists.data
    dbsLists = form.dbsLists.data
    content = form.content.data

    report = Report(tid=vulId, vulURL=vulURL, homeTitle=homeTitle, homeLink=homeLink, DBInfo=DBInfo,
        vulId=vulParId, UserLists=UserLists, dbsLists=dbsLists, content=content) 

    db.session.add(report) 

    vul = Task.query.filter(Task.tid==vulId).first()
    vul.detectFlag = 2   
    db.session.commit()  

    return redirect("/report/view/"+str(vulId))
else:
    title = "Report-{}".format(vulId)
    vul = Task.query.filter(Task.tid==vulId).first()  

    vulTitle = ""
    pageTitle = ""
    try:
        pageTitle = httpHandle.getTitle(vul.url)
    except Exception, e:
        pageTitle = "Get title error!{}".format(e)

    encodeType = chardet.detect(pageTitle) 
    if encodeType["encoding"] == "GB2312":
        vulTitle =  pageTitle.decode("gb2312",'ignore')
    else:
        vulTitle = pageTitle.decode("utf-8",'ignore') 

    form.vulURL.process_data(vul.getURL())
    form.content.process_data(vul.getRawData()) 
    return render_template("makeReport.html", title=title, vul=vul, vulTitle=vulTitle, form=form)
3205 次点击
所在节点    Python
17 条回复
janxin
2015-12-30 09:16:53 +08:00
db.session.commit()试一下?
saberlion
2015-12-30 10:10:20 +08:00
sqlalchemy 的 debug 打开有真相
lixiaohan
2015-12-30 10:39:38 +08:00
你要先 commit db.session.commit()
florije
2015-12-30 10:51:02 +08:00
还是 debug 看信息吧,一般会显示出来所有的生成 sql 命令,看下插入或者查找失败的失败的 sql 是哪里不对,另, ls 小伙伴们人家 LZ 已经写了 commit 了……
janxin
2015-12-30 11:27:25 +08:00
@florije 确实没注意到下面的具体代码里有,我的锅
nersle
2015-12-30 12:07:34 +08:00
@saberlion MySQL 都插入成功了呢,只是需要在 model.py 的 main 函数中执行
```report = Report.query.filter(Report.tid==vulId).first()
再在 views.py 中运行
```report = Report.query.filter(Report.tid==vulId).first()
才会成功,求解
nersle
2015-12-30 12:08:05 +08:00
@florije Mysql 插入成功了....确实有数据
tanywei
2015-12-30 12:28:27 +08:00
这种都是 session 的问题吧, 先 flush 一把看看。
nersle
2015-12-30 12:59:45 +08:00
@tanywei 怎么 flush ? commit 还不行么?
nersle
2015-12-30 13:08:31 +08:00
刚测试发现,或者重新运行 Flask 应用也行,真是好奇怪,修改一条记录,就得重新运行一次
tanywei
2015-12-30 13:38:07 +08:00
flask sqlalchemy scoped_session 搜搜看。
janxin
2015-12-30 13:57:24 +08:00
你先试试
```python
app.config['SQLALCHEMY_ECHO'] = True
app.config['SQLALCHEMY_RECORD_QUERIES'] = True
```
看一下输出
nersle
2015-12-30 15:35:50 +08:00
@janxin 配置了,没有用, SQL 语句也没有问题,确实插入数据库了
blueegg
2015-12-30 16:00:51 +08:00
我晕,终于可以回复了。加这一句试试 db.session.remove()
hahastudio
2015-12-30 16:30:25 +08:00
alexapollo
2015-12-30 16:38:55 +08:00
遇到这种坑的时候非常庆幸自己是手动管理的……
florije
2015-12-30 16:55:47 +08:00
能切到 sqlite 然后把相关部分代码整理下放 github 上吗?

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/247083

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX