DetachedInstanceError:Instance<>不受会话约束;属性刷新操作无法继续

时间:2016-10-15 22:19:26

标签: python flask flask-sqlalchemy

感谢阅读。

我可能正在做一些非智能的事情 - 我是一个相对较新的数据库用户。

这是错误的完整堆栈跟踪:

sqlalchemy.orm.exc.DetachedInstanceError

DetachedInstanceError: Instance <Event at 0x7f514c79bc10> is not bound to a Session; attribute refresh operation cannot proceed
Traceback (most recent call last)

    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2000, in __call__

    return self.wsgi_app(environ, start_response)

    File "/usr/local/lib/python2.7/dist-packages/werkzeug/contrib/fixers.py", line 152, in __call__

    return self.app(environ, start_response)

    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1991, in wsgi_app

    response = self.make_response(self.handle_exception(e))

    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1567, in handle_exception

    reraise(exc_type, exc_value, tb)

    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app

    response = self.full_dispatch_request()

    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request

    rv = self.handle_user_exception(e)

    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception

    reraise(exc_type, exc_value, tb)

    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request

    rv = self.dispatch_request()

    File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request

    return self.view_functions[rule.endpoint](**req.view_args)

    File "/home/rodda/workspace/turbo-guacamole/app/authentication_methods.py", line 44, in check_user

    return func(*args, **kwargs)

    File "/home/rodda/workspace/turbo-guacamole/app/admin_views.py", line 63, in admin_publicize

    start_time = event.start_time

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/attributes.py", line 237, in __get__

    return self.impl.get(instance_state(instance), dict_)

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/attributes.py", line 578, in get

    value = state._load_expired(state, passive)

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/state.py", line 474, in _load_expired

    self.manager.deferred_scalar_loader(self, toload)

    File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/loading.py", line 610, in load_scalar_attributes

    (state_str(state)))

    DetachedInstanceError: Instance <Event at 0x7f514c79bc10> is not bound to a Session; attribute refresh operation cannot proceed

生成此错误的代码如下:

events = models.Event.query.filter_by(classification = 'ais').all()
for event in events:
  start_time = event.start_time

我从Flask应用程序的许多其他部分查询事件,我没有遇到问题。只有当我查询特定事件并将其分类设置为ais时,才会出现此问题。

然而,最有趣的是,当我在Python shell中运行相同的精确代码时,它不会引发错误。

我如何根据创建其他事件的方式创建这些事件没有区别。

想法?

非常感谢。

P.S。我最好不要重新加载所有这些事件(其中大约有400个)。如果有办法解决这个问题而不重新创建所有优先选择的方法。

1 个答案:

答案 0 :(得分:0)

events是一个列表

 start_time = []
 events = models.Event.query.filter_by(classification = 'ais').all()
 for event in events:
     start_time.append(event.start_time)  

这将为您提供所有event.start_time的列表。