How can I arrange objects according to some attribute of a child element in sqlalchemy?

Here is the situation: I have a parent model BlogPost. He has a lot of Comments. I want the list to be BlogPostsorted by its creation date Comment. That is, a blog post that has the most recent comment should be at the top of the list. Is this possible with SQLAlchemy?

+3
source share
2 answers

http://www.sqlalchemy.org/docs/05/mappers.html#controlling-ordering

Starting with version 0.5, ORM does not generate an order for any request unless explicitly configured.

" " , , order_by ():

+3

, ORM, GHZ , . sqlalchemy, , BlogPost.comments , :

session.query(BlogPost).order_by (BlogPost.comments.creationDate.desc())

:

session.query(BlogPost).join().order_by (Comments.creationDate.desc())

+1

Source: https://habr.com/ru/post/1702419/


All Articles