Open williamjulianvicary opened 5 years ago
Thank you for the suggestion and the thorough report. I agree that this needs to be documented better. It might be appropriate to document this in marshmallow-sqlalchemy then link to those docs in flask-marshmallow.
Would you be up for sending a PR?
Slightly-related: https://github.com/marshmallow-code/marshmallow-sqlalchemy/pull/192/
Thanks @williamjulianvicary your post helped me alot
I have tried to declare models in app.init.py,and it works. like this: app. init.py db = SQLAlchemy() ma=Marshmallow()
from app.resources.models import Resource,BluePrint
from app.roles.models import Role
from app.groups.models import Group from app.users.models import User from app.permissions.models import Permission
from app.film.models import FilmAoiRecord
Firstly, thank you for the great extension!!
I've ran into an error that I'm sure others will have ran into, it may be worth updating the docs with a warning about it.
Our structure was as follows:
Some background - with SQLAlchemy, with separate models, you need to import them all at runtime, before the DB is initialised to avoid circular dependancies within relationships.
When the
UserSchema(ma.ModelSchema)
is hit during importfrom app.models import *
(in bootstrap) this initialises the models and attempts to execute the relationships. At this stage, we may not have a relationship requirement (which SQLAlchemy avoids using string based relationships) however as thema.ModelSchema
initialises the models it creates errors such as this:and, on subsequent loads:
The solution to this is to simply build the UserSchemas in a different import namespace, we've now got:
And no more circular issues - hopefully this helps someone else, went around in circles (pun intended) for a few hours before I realised it was the ModelSchema causing it.
Could the docs be updated to make a point of explaining that the ModelSchema initialises the model, and therefore it's a good idea for them to be in separate import destinations?