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 == [])
无效
答案 0 :(得分:2)
从这里找到答案:https://groups.google.com/d/msg/sqlalchemy/Ow0bb6HvczU/VVQbtd7MnZkJ
所以解决方案是
session.query(BlogPost).filter(BlogPost.authors.any())