简体   繁体   中英

SQLAlchemy Flask filter query to combine results from two models

I am trying to order a user's thanks(posts) by date_registered.

At the moment, there are two queries to get two lists, then manually combine and re-order. A user can be a giver of thank or a receiver. A thank has always a single giver, however might have multiple receivers. Therefore I have a Thank and a ThankReceivedByUser models to achive results.

class Thank(db.Model):

    id = db.Column(db.Integer, primary_key = True)
    giver_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable = False)
    status = db.Column(db.SmallInteger, nullable = False) 
    date_registered = db.Column(db.DateTime, nullable = False)

class ThankReceivedByUser(db.Model):

    id = db.Column(db.Integer, primary_key = True)
    thank_id = db.Column(db.Integer, db.ForeignKey("thank.id"), nullable = False)
    receiver_id = db.Column(db.Integer, db.ForeignKey("user.id"), nullable = False)
    status = db.Column(db.SmallInteger, nullable = False) 
    date_registered = db.Column(db.DateTime, nullable = False)

It would be great to get some hints on constructing a single query if possible.

I came up with a solution to this problem:

thanks = Thank.query\
    .join(ThanksReceivedByUser)\
    .filter(or_(Thank.giver_id == user.id, 
                ThankReceivedByUser.receiver_id == user.id))\
    .order_by(Thank.date_registered).all()

If I understand your question correctly, you want to combine thanks given and received by a user in a single query. If so, you need to UNION two queries, one of which selects given thanks, while another received ones. Then you can order the results of this combined query. Here's how your query would look like:

# user_id contains a value of some user's id.
given_q = db.session.query(Thank).filter_by(giver_id=user_id)
received_q = db.session.query(Thank).join(ThankReceivedByUser).\
    filter(ThankReceivedByUser.receiver_id == user_id)
q = given_q.union(received_q).order_by(Thank.date_registered)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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