I'm having a trouble with appending my values to an association table. I haven't fixed this issue ever since. I don't know if it's a problem with the relationship or with other things.
Here's my association table
item_tags_association_table = db.Table('item_tags_associations', db.Model.metadata,
db.Column('item_id', db.Integer, db.ForeignKey('items.id')),
db.Column('tag_id', db.Integer, db.ForeignKey('tags.id')))
Here's my Item model
class Item(db.Model):
# 1. Set the table name in plural form
__tablename__ = 'items'
# 2. Implemantation Attributes()
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80),nullable=False)
description = db.Column(db.Text,nullable=False)
quantity = db.Column(db.Integer,nullable=False)
price = db.Column(db.Integer,nullable=False)
avatar_url = db.Column(db.String(500))
bio = db.Column(db.String(240))
#2019/07/23
category_id = db.Column(db.Integer, db.ForeignKey('categories.id'))
category = db.relationship("Category", back_populates="items")
tags = db.relationship("Tag", secondary=item_tags_association_table,
back_populates="items")
And here's my function to process the append that is actually inside the class Item as well.
def insert_tags(self, tag_list):
for tag in tag_list:
self.tags.append(tag)
Here's the Tag model
class Tag(db.Model):
__tablename__='tags'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80),nullable=False)
items = db.relationship("Item",
secondary=item_tags_association_table,
back_populates="tags")