How to query many-to-many based on some constraints in flask sqlalchemy?

£可爱£侵袭症+ 提交于 2019-12-02 08:31:57

You need to join the tables you will query, so that filtering one will filter the combined row associated with the other. Since you have defined a relationship between the two models, you can join on it rather than specifying a join condition manually.

Item.query.join(Item.users).filter(User.name == 'bob')
Item.query.join(Item.users).filter(User.name == 'bob', Item.name == 'shark')

Working with relationships and joins is covered in the comprehensive tutorial in the SQLAlchemy docs.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!