pythonflaskflask-sqlalchemyflask-marshmallow

raise ValueError("Deserialization requires a session")


indeed a very weird error I am dealing with. Weird because the same project is working fine when I am running it on localhost in pyCharm, however, when I uploaded all the files to EC2 server. I got the following error.

The project is running fine so I am able to host the application however when I send a post request I get HTTP ERROR CODE 500 as a response and following error in the console.

If someone could help me rectify this, I would be grateful

    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

In my application, this is where the error was started.

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())

Solution

  • As discussed in the comments, here is the solution so it can be helpful for other people:

    You need to define a session and pass it to the load function like so:

    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)