【问题标题】:flask sqlalchemy query filter relationshipflask sqlalchemy 查询过滤关系
【发布时间】:2015-09-08 08:57:59
【问题描述】:

我有 2 个模型:Post 和 Comment,一个帖子有很多 cmets,如何过滤 cmets.count() 小于 2 的帖子?

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

非常感谢!!

【问题讨论】:

    标签: python relationship flask-sqlalchemy


    【解决方案1】:

    我认为没有比扫描整个表格更好的解决方案了。

    为了提高效率,您可以在帖子表中添加一个字段(带索引)num_comments,并在有新评论时更新它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-03-10
      • 1970-01-01
      • 1970-01-01
      • 2016-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多