出錯(cuò)位置大概再這里 (flask項(xiàng)目)def order_record(): id=request.args.get("id") code=request.args.get("code") r=Record()
r.id=id
order.code=code
db.session.add(order)
db.session.commit()
return jsonify(status=1)sqlalchemy.exc.InvalidRequestError: This Session's transaction has been rolled back due to a previous exception during flush. To begin a new transaction with this Session, first issue Session.rollback(). Original exception was: (IntegrityError) PRIMARY KEY must be unique原因感覺是當(dāng)數(shù)據(jù)提交的時(shí)候因?yàn)榫W(wǎng)絡(luò)的問(wèn)題或者用戶的問(wèn)題提交了多次相同的數(shù)據(jù)導(dǎo)致了unique的錯(cuò)誤。但是我好奇的是日志里會(huì)重復(fù)報(bào)告上面這個(gè)錯(cuò)誤描述很多次幾十次甚至幾百次。解決這個(gè)問(wèn)題,貌似只要再寫入的時(shí)候確認(rèn)是否已經(jīng)存在就可以了,但是我不太理解的是什么日志中會(huì)一直重復(fù)報(bào)這個(gè)InvalidRequestError錯(cuò)誤。另外。關(guān)于上面這斷代碼,除了寫入前先select以下之外,有沒有更優(yōu)美一點(diǎn)的做法? try catch?
- 2 回答
- 0 關(guān)注
- 228 瀏覽
添加回答
舉報(bào)
0/150
提交
取消