【发布时间】:2019-07-26 08:41:06
【问题描述】:
我在将值附加到关联表时遇到问题。从那以后我一直没有解决这个问题。不知道是感情问题还是其他方面的问题。
这是我的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')))
这是我的Item 模型
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")
这是我处理实际在类 Item 中的追加的函数。
def insert_tags(self, tag_list):
for tag in tag_list:
self.tags.append(tag)
这是Tag 模型
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")
【问题讨论】:
标签: python flask sqlalchemy flask-sqlalchemy