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)
With this setup, I can successfully query all the tags
for a given post
and vice versa, but I dont know to add a new association for a given post
and tag
.
Please see the below screenshot on how I query the related tags
and posts
off of each other.
If there is anything that I am missing here, please let me know.
Thanks