flask-graphene-sqlalchemy
flask-graphene-sqlalchemy copied to clipboard
Thanks for the everything but still need some more help please
@alexisrolland , thank you for everything. I really liked the Wiki page which was extremely thorough. Much appreciated!!
That said, I have a doubt as well. Please consider the following use case.
There is a post
model as well as a tag
model. Both of them have a many to many relationship between them. A post
can have multiple tags
while a tag
can have multiple posts
.
In order to attain this use case, I have implemented a mapping
table called, PostTag
and it looks like as follows
from database.base import Base
from sqlalchemy import Column, ForeignKey, Integer
from sqlalchemy.orm import relationship
from .model_post import ModelPost
from .model_tag import ModelTag
class PostTag(Base):
__tablename__ = 'posttag'
post_id = Column("post_id",Integer, ForeignKey('post.id'), primary_key = True)
tag_id = Column("tag_id",Integer, ForeignKey('tag.id'), primary_key = True)
Now, I dont know how to update my schema in such a manner that I can get a tagList
when I query for it. Notice that in the screenshot below, there is no tagList
and an error is being displayed on the screen. This is happening when I am querying for all the posts
related data.
Can you please let me know what I should be doing in this case ?
Thanks once again.
With Regards Gagan
Hello @gj1118
I have never tried many-to-many relationships in the context of this project so I do not have an already made solution for you. Sorry. However if you find a solution to your problem do not hesitate to do a PR on this repository to enrich it. It would be very useful.
Some ideas below:
I assume you have the following files model_post.py
, model_tag.py
and model_post_tag.py
. Have you properly defined your relationships in model_post.py
and model_tag.py
like this: https://github.com/alexisrolland/flask-graphene-sqlalchemy/blob/master/example/database/model_planet.py#L26
If you do, I would I assume Graphene would generate automatically fields so that you can query from post
to postTag
to tag
and vice versa
Alexis