zoukankan      html  css  js  c++  java
  • 解决 SQLAlchemy 提示 Instance is not bound to a Session 错误的问题

    在使用 SQLAlchemy 的过程中,有时会出现下列错误:

    • Parent instance '<User at 0x2b45b53509d0>' is not bound to a Session; lazy load operation of attribute cannot proceed
    • Instance '<User at 0x2b45b53509d0>' is not bound to a Session; attribute refresh operation cannot proceed

    出现以上错误的原因是因为:session 已经被提交,导致操作的 model 对象已经不在当前 session 中了。 解决的办法就是:把对象重新加入到当前 session 中:

    def foo(user):
        # do something...
        session.commit()
    
    user = session.query(User).filter_by(name='Jim').first()
    foo(user)
    print user in session  # False
    print user.name  # DetachedInstanceError: Instance <User at 0x2b45b53509d0> is not bound to a Session
    
    user = session.merge(user)
    print user in session  # True
    print user.name  # Jim
    session.refresh(user)
    print user.name  # Eric
  • 相关阅读:
    c语言7-4
    c语言 7-4
    dfs
    dfs
    二进制搜索
    BFS
    搜索多层图
    八皇后
    线段树-周长并
    线段树
  • 原文地址:https://www.cnblogs.com/btxlc/p/12400897.html
Copyright © 2011-2022 走看看