1

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")

1 Answer 1

1

Update: I answered the problem by myself, so the problem is that I'm trying to append a primary key rather than appending the object itself. so in my views.py I created an object to get the data using the primary key that was passed by the input.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.