如何查询多对多关系中的空记录 [英] How to query for empty records in many to many relation

查看:61
本文介绍了如何查询多对多关系中的空记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

class BlogPost(SchemaBase):
  id = Column(Integer, primary_key=True)
  name    = Column(String,  unique=True)
  authors = relationship('Authors', secondary='authors_to_blog_post')
  __tablename__ = 'blogpost'

class Authors(SchemaBase):
  id = Column(Integer, primary_key=True)
  name    = Column(String,  unique=True)
  __tablename__ = 'author'

authors_to_blog_post = Table('authors_to_blog_post', Base.metadata,
    Column('author_id', Integer, ForeignKey('author.id')),
    Column('blogpost_id', Integer, ForeignKey('blogpost.id'))
    )

现在如何在没有任何作者的情况下查询所有博客文章? session.query(BlogPost).filter(BlogPost.authors == [])不起作用

Now how to query for all blogposts without any author? session.query(BlogPost).filter(BlogPost.authors == []) doesnt work

推荐答案

从此处找到答案:所以解决方法是

session.query(BlogPost).filter(BlogPost.authors.any())

这篇关于如何查询多对多关系中的空记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆