How to query for empty records in many to many relation

我是研究僧i 提交于 2019-12-11 08:44:15

问题


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

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


回答1:


Found answer from here: https://groups.google.com/d/msg/sqlalchemy/Ow0bb6HvczU/VVQbtd7MnZkJ

So the solution is

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


来源:https://stackoverflow.com/questions/23814431/how-to-query-for-empty-records-in-many-to-many-relation

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