Cannot use schema.load() using marshmallow when resource, schema and model is not in app.py
When model, schema, and resource is declared in the app.py everything works fine, but when I change the folder structure to this:
├───models
│ AuthorModel.py
│ BookModel.py
│
├───resources
│ authorResource.py
│ bookResource.py
│
├───schemas
│ AuthorSchema.py
│ BookSchema.py
│
│ app.py
│ db.py
│ ma.py
│ Pipfile
│ Pipfile.lock
error is thrown: AttributeError: 'DummySession' object has no attribute 'query'
There was a suggestion to pass db session when loading schema my_schema.load(request.json(), session=db.session). Others had some success with that, however, in this case, it doesn't work.
I have created a repo with marshmallow_sqlalchemy Books-Authors example that is used in documentation.
UPDATE #1: When model, schema, and resource are moved to a single file all.py error is still thrown. Can't figure out why it works when everything is stored in app.py. It is likely that when app.py references the model, schema, or resource, db is not initialized.
UPDATE #2: I am checking the schema.session just before load and it does have an sqlalchemy.orm.scoping.scoped_session instead of flask_marshmallow.sqla.DummySession object
I've unfortunately run into the same issue as you. Did you solve it?
I didn't. Instead of using marshmallow-sqlalchemy I decided to use pure marshmallow. I didn't run into any problems using this library. Sure you have to define schemas yourself but that gives you a ton of flexibility and if you run into some issues you don't have to fight with the library to fix them.
Thank you. That was very helpful, I'm leaning towards that as well.
On Mon, Jul 13, 2020 at 3:00 PM Evaldas-B [email protected] wrote:
I didn't. Instead of using marshmallow-sqlalchemy I decided to use pure marshmallow. I didn't run into any problems using this library. Sure you have to define schemas yourself but that gives you a ton of flexibility and if you run into some issues you don't have to fight with the library to fix them.
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues/312#issuecomment-657734793, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJDVCBOI7QGRRDGALKQ44BLR3NKT7ANCNFSM4NBYBSMA .
I ran into the same issue, and it appears to happen when I user non-numeric primary key. (I used uuid string) A solution is to force flask-marshmallow use your db.session. Like this
item_info_schema.load(item_data, session=db.session)
Thank you. Unfortunately I've already moved on with standard Marshmallow, but I'm sure the next person reading this will be thankful!
On Fri, Sep 4, 2020 at 11:04 AM kaito-albert [email protected] wrote:
I ran into the same issue, and it appears to happen when I user non-numeric primary key. (I used uuid string) A solution is to force flask-marshmallow use your db.session. Like this
item_info_schema.load(item_data, session=db.session)
— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues/312#issuecomment-687205318, or unsubscribe https://github.com/notifications/unsubscribe-auth/AJDVCBNMEMK6WF7ZQNQIHJ3SED6WLANCNFSM4NBYBSMA .
I ran into this same issue while doing a local project. I was able to get it to work, and here's some of the key points I ran into:
Most importantly, you MUST import your model and schema objects AFTER you have initialized your app. When the model and schema class objects are created, they will take a reference to your SQLAlchemy() and Marshmallow() objects, respectively, at the time they are imported. So if they're not initialized with the Flask application at that time, you end up without any context, and that results in the DummySession being found. I know forcing the database session didn't work for me either; I didn't try to force the marshmallow session (if there even is one). Also remember, if you're using pylint, to disable wrong-import-position so you don't get dinged.
You still have to establish the app_context on whatever it is that you run. Otherwise, you'll get something like "RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/." when you try to do queries or any sort of similar action on your model.