flask sqlalchemy 查询过滤关系
flask sqlalchemy query filter relationship
我有2个模型:Post和Comment,一个post有很多评论,如何过滤comments.count()小于2的posts ?
Post: id(primary key), body, author_id, comments
Comment: id(primary key), body, author_id, post_id
其中,
comments = db.relationship('Comment', backref='post', lazy='dynamic')
非常感谢!!
我认为没有比扫描整个 table 更好的解决方案了。
为了提高效率,您可以在 post table 中添加一个字段(带索引)num_comments
,并在有新评论时更新它。
我有2个模型:Post和Comment,一个post有很多评论,如何过滤comments.count()小于2的posts ?
Post: id(primary key), body, author_id, comments
Comment: id(primary key), body, author_id, post_id
其中,
comments = db.relationship('Comment', backref='post', lazy='dynamic')
非常感谢!!
我认为没有比扫描整个 table 更好的解决方案了。
为了提高效率,您可以在 post table 中添加一个字段(带索引)num_comments
,并在有新评论时更新它。