Count number of rows in a many-to-many relationship (SQLAlchemy)

后端 未结 1 1441
南笙
南笙 2021-01-11 13:43

I have a many-to-many relationship between say blog entries and tags. Now I want to know how many entries a specific tag has.

Imagine the following models (simplifie

相关标签:
1条回答
  • 2021-01-11 14:02
    session.query(Entry).join(Entry.tags).filter(Tag.id==1).count()
    

    or if you have a Tag already

    session.query(Entry).with_parent(mytag, "entries").count()
    
    0 讨论(0)
提交回复
热议问题