-
Notifications
You must be signed in to change notification settings - Fork 59
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
'DummySession' object has no attribute 'query' #44
Comments
Hi @medavis, did you happen to solve this problem? I encountered it myself today and I have not found a suitable solution (yet). |
I am afraid I have not. We have just not upgraded yet, and pressed ahead. I do wonder however if the cause of my problem had to do with a configuration problem. We have found that the DummySession object is there even in the older code, and the error can be generated even with the older code if one accidentally ends up calling the marshmallow code before the application and session have been configured. So, my best suggestion is to double check for that first. Malcolm From: Andreas [mailto:[email protected]] Hi @medavishttps://github.com/medavis, did you happen to solve this problem? I encountered it myself today and I have not found a suitable solution (yet). — This message (including any attachments) may contain confidential, proprietary, privileged and/or private information. The information is intended to be for the use of the individual or entity designated above. If you are not the intended recipient of this message, please notify the sender immediately, and delete the message and any attachments. Any disclosure, reproduction, distribution or other use of this message or any attachments by an individual or entity other than the intended recipient is prohibited. |
I have now found a workaround, which is to pass a database session explicitly to the load method like this:
Where db.session comes from flask_sqlalchemy. EDIT: but that does not seem to be what you tried to do. |
Thanks Sent from my phone. I have now found a workaround, which is to pass a database session explicitly to the load method like this: my_schema.load(request.json, session=db.session) Where db.session comes from flask_sqlalchemy. Hope this helps. — This message (including any attachments) may contain confidential, proprietary, privileged and/or private information. The information is intended to be for the use of the individual or entity designated above. If you are not the intended recipient of this message, please notify the sender immediately, and delete the message and any attachments. Any disclosure, reproduction, distribution or other use of this message or any attachments by an individual or entity other than the intended recipient is prohibited. |
@AndreasGB, have you by any chance found a way to deal with nested schemas? Or, did anyone found a way to deal with foreign keys on SQLAlchemy models and avoid this issue with Marshmallow? (perhaps I'm just doing it the wrong way..?) Thank you |
Answer to my own question is here: The session simply has to be set when declaring the schemas. |
I had the same issue, and this does work. Thanks |
I'm having the same issue ( AttributeError: 'DummySession' object has no attribute 'query' ), but only when i run tests using Correction, it appears that the problem manifests itself only when i run unit tests (pytest, flask test_client) |
Same for me. |
I am running into this problem as well with pytest, flask_test_client and nested Schema's (not with simple fields). There seems to be no problem at all when running the code in development or production, only in testing does this error show up. |
I know this is incredibly late to the game, but just in case someone else is searching, as I had been for some considerable time: If you are using marshmallow_sqlalchemy in addition to flask_marshmallow (if you're using flask_sqlalchemy then you probably are), then flask_marshmallow's usage of Nested is not going to work in unit testing. You must use the marshmallow_sqlalchemy.fields.Nested in conjunction with an override on session during your load in order to get this to work. Either interpretation will work for a production run, which is why it seems a little tough to track down. See marshmallow-code/marshmallow-sqlalchemy#298 for more info. |
This workaround has worked for me too. |
There is another workaround that doesn't require passing # app/schemas.py
from app.extensions import db # the Flask-SQLAlchemy extension instance
from app.extensions import ma as flask_ma # the Flask-Marshmallow extension instance
# this pattern also works with flask_ma.SQLAlchemyAutoSchema
class SQLAlchemySchema(flask_ma.SQLAlchemySchema):
class Meta(flask_ma.SQLAlchemySchema.Meta):
sqla_session = db.session
# all of your model schemas should inherit from your schema base class
class YourSchema(SQLAlchemySchema):
class Meta(SQLAlchemySchema.Meta): # <- note Meta must *also* subclass the base class's Meta
model = YourModel
# ... field definitions Upstream devs: The cause of the original bug has to do with the fact that |
ma.init_app(app) replace DummySession() by db.session but its too late, schemas have already been created, so replace it as soon as ma have been initialized. marshmallow-code/flask-marshmallow#44 marshmallow-code/flask-marshmallow#74 marshmallow-code/flask-marshmallow#111
ma.init_app(app) replace DummySession() by db.session but its too late, schemas have already been created, so replace it as soon as ma have been initialized. marshmallow-code/flask-marshmallow#44 marshmallow-code/flask-marshmallow#74 marshmallow-code/flask-marshmallow#111
ma.init_app(app) replace DummySession() by db.session but its too late, schemas have already been created, so replace it as soon as ma have been initialized. marshmallow-code/flask-marshmallow#44 marshmallow-code/flask-marshmallow#74 marshmallow-code/flask-marshmallow#111
I am working on a new project and was hoping that all of this was cleared up. But once again I am encountering the dreaded "AttributeError: 'DummySession' object has no attribute 'get'" error. The error arises as soon as one of my nested, many=true, lists contains what should be a preexisting object. I have tried getting the correct session into the schema object in a variety of ways, but I can still see in the debugger that the nested schema's session attribute points to a DummySession object. Things I have tried:
Some other things to note. I do have What do I need to do to get the nested Schema to use the right session? (Just as an inconsequential note, I am the medavis who started this issue back in 2016.) |
Ah. I have found the combination that works. If I set the |
Following advice from the internet to avoid flaky errors we're seeing occasionally in our unit tests[^1] [^1]: marshmallow-code/flask-marshmallow#44 (comment)
We have been using flask-marshmallow 0.6.0 with marshmallow-sqlalchemy 0.3.0 for some time now, and been quite happy. However, in trying to upgrade our packages we have encountered the error message above.
It appears that marshmallow-sqlalchemy is now trying to actually manage adding/merging the models with the session. Personally, I don't want that. I want marshmallow to handle the deserialization and leave it to me to decide when and how I want to add or merge the model with the session, as we have been doing for some time. I have suggested on their issue board where others had brought up the issue that it would be nice if their code just skipped the session management parts if the session was none (see https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues/62). If they did that, then you wouldn't need to have a DummySession class at all. I do not know how amenable they will be to that suggestion.
The alternative is unfortunately to have to make DummySession implement methods to avoid generating errors, but this requires not just the query method but then it would appear filter_by, one, and first.
Or perhaps there is an alternative workaround that you already have in place. If so, I would be anxious to hear it.
Thanks.
Oh the full traceback is
Traceback (most recent call last):
File "tests.py", line 245, in runTest
(obj, errors) = schema.loads(example_str[name])
File "/home/davism/Src/atsdb/venv/lib/python2.7/site-packages/marshmallow/schema.py", line 564, in loads
return self.load(data, many=many, partial=partial)
File "/home/davism/Src/atsdb/venv/lib/python2.7/site-packages/marshmallow_sqlalchemy/schema.py", line 186, in load
return super(ModelSchema, self).load(data, _args, *_kwargs)
File "/home/davism/Src/atsdb/venv/lib/python2.7/site-packages/marshmallow/schema.py", line 542, in load
result, errors = self._do_load(data, many, partial=partial, postprocess=True)
File "/home/davism/Src/atsdb/venv/lib/python2.7/site-packages/marshmallow/schema.py", line 646, in _do_load
result = self._invoke_load_processors(POST_LOAD, result, many, original_data=data)
File "/home/davism/Src/atsdb/venv/lib/python2.7/site-packages/marshmallow/schema.py", line 767, in _invoke_load_processors
data=data, many=many, original_data=original_data)
File "/home/davism/Src/atsdb/venv/lib/python2.7/site-packages/marshmallow/schema.py", line 865, in _invoke_processors
data = utils.if_none(processor(data), data)
File "/home/davism/Src/atsdb/venv/lib/python2.7/site-packages/marshmallow_sqlalchemy/schema.py", line 169, in make_instance
instance = self.instance or self.get_instance(data)
File "/home/davism/Src/atsdb/venv/lib/python2.7/site-packages/marshmallow_sqlalchemy/schema.py", line 154, in get_instance
return self.session.query(
AttributeError: 'DummySession' object has no attribute 'query'
The text was updated successfully, but these errors were encountered: