使用 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"])
return redirect("/vul/list")
def viewReport(vulId=-1):
if vulId < 0:
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)
1
janxin 2015-12-30 09:16:53 +08:00
db.session.commit()试一下?
|
2
saberlion 2015-12-30 10:10:20 +08:00
sqlalchemy 的 debug 打开有真相
|
3
lixiaohan 2015-12-30 10:39:38 +08:00
你要先 commit db.session.commit()
|
4
florije 2015-12-30 10:51:02 +08:00
还是 debug 看信息吧,一般会显示出来所有的生成 sql 命令,看下插入或者查找失败的失败的 sql 是哪里不对,另, ls 小伙伴们人家 LZ 已经写了 commit 了……
|
6
nersle OP |
8
tanywei 2015-12-30 12:28:27 +08:00
这种都是 session 的问题吧, 先 flush 一把看看。
|
10
nersle OP 刚测试发现,或者重新运行 Flask 应用也行,真是好奇怪,修改一条记录,就得重新运行一次
|
11
tanywei 2015-12-30 13:38:07 +08:00
flask sqlalchemy scoped_session 搜搜看。
|
12
janxin 2015-12-30 13:57:24 +08:00
你先试试
```python app.config['SQLALCHEMY_ECHO'] = True app.config['SQLALCHEMY_RECORD_QUERIES'] = True ``` 看一下输出 |
14
blueegg 2015-12-30 16:00:51 +08:00
我晕,终于可以回复了。加这一句试试 db.session.remove()
|
15
hahastudio 2015-12-30 16:30:25 +08:00
|
16
alexapollo 2015-12-30 16:38:55 +08:00
遇到这种坑的时候非常庆幸自己是手动管理的……
|
17
florije 2015-12-30 16:55:47 +08:00
能切到 sqlite 然后把相关部分代码整理下放 github 上吗?
|