✅ 操作成功!

raised as a result of Query

发布时间:2023-06-12 作者:admin 来源:文学

raised as a result of Query

raised as a result of Query

tornado做一个商城,进入联系卖家聊天窗时抛出异常"(raised as a result of Query-invoked autoflush; consider using a session.no_autoflush block if this flush is occurring prematurely)"

解决办法:使用  with session.no_autoflush:

示例:

抛异常代码:

me = session.query(User).filter_by(id=int(my_id)).first() he = session.query(User).filter_by(id=int(his_id)).first() # 获取聊天记录 records=session.query(ChatRecord).filter(or_(and_(ChatRecord.who_send==int(my_id),ChatRecord.who_recv==int(his_id)),and_(ChatRecord.who_send==int(his_id),ChatRecord.who_recv==int(my_id)))).order_by('id').all()

将数据库操作的代码用with session.no_autoflush:包裹,不自动刷新:

with session.no_autoflush: me = session.query(User).filter_by(id=int(my_id)).first() he = session.query(User).filter_by(id=int(his_id)).first() # 获取聊天记录 records = session.query(ChatRecord).filter(or_(and_(ChatRecord.who_send==int(my_id),ChatRecord.who_recv==int(his_id)),and_(ChatRecord.who_send==int(his_id),ChatRecord.who_recv==int(my_id)))).order_by('id').all()

 

👁️ 阅读量:0