Skip to content
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

Cannot use schema.load() using marshmallow when resource, schema and model is not in app.py #312

Open
Evaldas-B opened this issue May 15, 2020 · 6 comments

Comments

@Evaldas-B
Copy link

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

@Tommpa
Copy link

Tommpa commented Jul 13, 2020

I've unfortunately run into the same issue as you. Did you solve it?

@Evaldas-B
Copy link
Author

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.

@Tommpa
Copy link

Tommpa commented Jul 16, 2020 via email

@kaito-albert
Copy link

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)

@Tommpa
Copy link

Tommpa commented Sep 7, 2020 via email

@wschmrdr
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants