背景:一个主题可以对应多个产品,一个产品可以属于多个主题
我也不想使用第三张表,但是人家已经给我了 sql 文件,我只能使用,要不怎么读取人家的数据
class Theme_Product(db.Model):
__tablename__ = 'theme_product'
theme_id=db.Column(db.Integer,db.ForeignKey('theme.id'), primary_key=True)
product_id=db.Column(db.Integer,db.ForeignKey('product.id'), primary_key=True)
class Theme(db.Model):
__tablename__ = 'theme'
id = db.Column(db.Integer, primary_key=True)
productInTheme=db.relationship('Theme_Product',
foreign_keys=[Theme_Product.theme_id],
backref=db.backref('backTheme',lazy='joined'),
lazy='dynamic',
cascade='all,delete-orphan')
class Product(db.Model):
__tablename__ = 'product'
id = db.Column(db.Integer, primary_key=True)
theme=db.relationship('Theme_Product',
foreign_keys=[Theme_Product.product_id],
backref=db.backref('backProduct', lazy='joined'),
lazy='dynamic',
cascade='all,delete-orphan')
-----------------------跪求指点,如何查询--------------------------------
1
vZexc0m 2018-03-21 13:44:24 +08:00
```
tags = db.Table('article_tag', db.Column('article_id', db.Integer(), db.ForeignKey('article.id')), db.Column('tag_id', db.Integer(), db.ForeignKey('tag.id'))) class Article(db.Model): id = db.Column(db.Integer(), primary_key=True) title = db.Column(db.String(255)) text = db.Column(db.Text()) publish_time = db.Column(db.DateTime(), default=datetime.datetime.utcnow) comments = db.relationship("Comment", backref='article', lazy='dynamic') tags = db.relationship('Tag', secondary=tags, backref=db.backref('article', lazy='dynamic')) def json(self): return dict( id=self.id, title=self.title, text=self.text, publish_time=utc_local(self.publish_time), username=self.user.username, tags=[tag.name for tag in self.tags] ) def __str__(self): return "<Article {}>".format(self.title) def __repr__(self): return "<Article {}>".format(self.title) class Tag(db.Model): id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.String(255)) def __str__(self): return "<Tag {}>".format(self.name) def __repr__(self): return "<Tag {}>".format(self.name) ``` |