-
Notifications
You must be signed in to change notification settings - Fork 95
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
Comments
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 ***@***.***> 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
<#312 (comment)>,
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) 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 ***@***.***> 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
<#312 (comment)>,
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. |
When model, schema, and resource is declared in the app.py everything works fine, but when I change the folder structure to this:
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
The text was updated successfully, but these errors were encountered: