使用sqlalchemy 时报错 Instance '' is not bound to a Session

问题

问题.png
使用sqlalchemy 时报错 Instance '<User at 0x2b45b53509d0>' is not bound to a Session_第1张图片
错误写法.png

由于我在get_formatted_notice() 中使用了session去获取的但是我又在下面又使用了formatted_notice.template.output()
因为里头对应的关联的表找不到,并且不在session中,导致该错误

解决办法:

使用sqlalchemy 时报错 Instance '<User at 0x2b45b53509d0>' is not bound to a Session_第2张图片
图片.png

将session 包裹在这两行代码中

如果不是上述原因导致的可以使用session.merge()方法

merage使用例子

对应api
SQLALchemy session.merage

你可能感兴趣的:(使用sqlalchemy 时报错 Instance '' is not bound to a Session)