简体   繁体   English

加入并计入sql-alchemy

[英]Join and count in sql-alchemy

I have a model defined like so: 我有一个如此定义的模型:

class Article(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    authors = db.relationship('Author', secondary=articles_authors, backref=db.backref('articles', lazy='dynamic'))
    tags = db.relationship('Tag', secondary=articles_tags, backref=db.backref('articles', lazy='dynamic'))
    comments = db.relationship('Comment', backref=db.backref('article'), lazy='dynamic')
    creation_time = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    modification_time = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    title = db.Column(db.String(256), nullable=False)
    contents = db.Column(db.Text, nullable=False)

I'd like to create a query that would give me all articles joined with authors and tags and that would count comments related to the given article. 我想创建一个查询,它会给我所有与作者和标签相关联的文章,并且会计算与给定文章相关的评论。 That's what I figured out so far: 这就是我到目前为止所知道的:

Article.query.options(db.joinedload(Article.authors), db.joinedload(Article.tags)).all()

What gives me trouble is the counting part - I couldn't find any examples on how to do it. 给我带来麻烦的是计数部分 - 我找不到任何关于如何做的例子。 So how do I do it? 那我该怎么做?

EDIT: 编辑:

Query that doesn't work, but feels like the right direction: 查询不起作用,但感觉正确的方向:

subquery = db.session.query(Comment.article_id, func.count(Comment.id).label('comments_count'))\
            .group_by(Comment.article_id).subquery()

return db.session.query(Article, subquery.c.comments_count)\
    .outerjoin(subquery, Article.id == subquery.c.article_id)\
    .join(Tag).all()

The counting part in this case works ok, but I'm not getting tags and authors using this query. 在这种情况下计数部分工作正常,但我没有得到使用此查询的标签和作者。

EDIT2: EDIT2:

If it's not obvious - relation between article and tag is many-to-many: 如果它不明显 - 文章和标签之间的关系是多对多的:

articles_tags = db.Table('articles_tags',
    db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')),
    db.Column('article_id', db.Integer, db.ForeignKey('article.id'))
)

The same goes for article and author. 文章和作者也是如此。

EDIT: 编辑:

The answer: 答案:

subquery = db.session.query(Comment.article_id, func.count(Comment.id).label('comments_count'))\
            .group_by(Comment.article_id).subquery()

return db.session.query(Article, subquery.c.comments_count)\
    .outerjoin(subquery, Article.id == subquery.c.article_id)\
    .options(db.joinedload(Article.authors), db.joinedload(Article.tags)).all()

I think what you are looking for might be something like this(?) 我认为你在找什么可能是这样的(?)

(
    session.query(Article, func.count(Comment.id))
    .select_from(Comment)
    .join(Comment.article)
    .group_by(Article)
)

To get the authors and tags you should be able to just use .authors or .tags on the Article objects. 要获得作者和标签,您应该能够在Article对象上使用.authors.tags To load it eagerly, you could use joinedload or subqueryload. 要急切地加载它,您可以使用joinedload或subqueryload。

EDIT: 编辑:

(
    session.query(Article, func.count(Comment.id))
    .select_from(Comment)
    .join(Comment.article)
    .options(db.joinedload(Article.authors), db.joinedload(Article.tags))
    .group_by(Article)
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM