提高 ValueError("Deserialization requires a session")

raise ValueError("Deserialization requires a session")

我正在处理一个非常奇怪的错误。很奇怪,因为当我 运行 在 pyCharm 的本地主机上时,同一个项目工作正常,但是,当我将所有文件上传到 EC2 服务器时。我收到以下错误。

该项目 运行 很好,所以我可以托管该应用程序,但是当我发送 post 请求时,我收到 HTTP ERROR CODE 500 作为响应并在控制台中出现以下错误。

如果有人能帮我纠正这个问题,我将不胜感激

    Traceback (most recent call last):
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/app.py", line 2464, in __cal                                                                                                l__
    return self.wsgi_app(environ, start_response)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/app.py", line 2450, in wsgi_                                                                                                app
    response = self.handle_exception(e)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask_restful/__init__.py", line 2                                                                                                72, in error_router
    return original_handler(e)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/app.py", line 1867, in handl                                                                                                e_exception
    reraise(exc_type, exc_value, tb)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/_compat.py", line 38, in rer                                                                                                aise
    raise value.with_traceback(tb)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_                                                                                                app
    response = self.full_dispatch_request()
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/app.py", line 1952, in full_                                                                                                dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask_restful/__init__.py", line 2                                                                                                72, in error_router
    return original_handler(e)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/app.py", line 1821, in handl                                                                                                e_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/_compat.py", line 38, in rer                                                                                                aise
    raise value.with_traceback(tb)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/app.py", line 1950, in full_                                                                                                dispatch_request
    rv = self.dispatch_request()
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/app.py", line 1936, in dispa                                                                                                tch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask_restful/__init__.py", line 4                                                                                                68, in wrapper
    resp = resource(*args, **kwargs)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask/views.py", line 89, in view
    return self.dispatch_request(*args, **kwargs)
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/flask_restful/__init__.py", line 5                                                                                                83, in dispatch_request
    resp = meth(*args, **kwargs)
  File "/home/ubuntu/yAPI/resources/r_user.py", line 50, in post
    user = user_schema.load(request.get_json())
  File "/home/ubuntu/yAPI/yenv/lib/python3.6/site-packages/marshmallow_sqlalchemy/schema/load                                                                                                _instance_mixin.py", line 89, in load
    raise ValueError("Deserialization requires a session")
ValueError: Deserialization requires a session

在我的应用程序中,这是错误开始的地方。

class UserRegister(Resource):

    @classmethod
    def post(cls):
        #        the 'load' function in marshmallow will use the data to create usermodel object
        user = user_schema.load(request.get_json())

正如评论中所讨论的,这里是解决方案,对其他人有帮助:

您需要像这样定义一个会话并将其传递给加载函数:

from sqlalchemy import engine #thanks to your comment
from sqlalchemy.orm import scoped_session, sessionmaker



class UserRegister(Resource):

    @classmethod
    def post(cls):
        #        the 'load' function in marshmallow will use the data to create usermodel object
        sess = scoped_session(sessionmaker(bind=engine))
        user = user_schema.load(request.get_json(), sess)