Skip to content

Instantly share code, notes, and snippets.

@alexanderjulo
Created December 4, 2012 14:59

Revisions

  1. Alexander Jung-Loddenkemper created this gist Dec 4, 2012.
    10 changes: 10 additions & 0 deletions many-to-many-common-finder.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    def find_common(*user_ids):
    queries = []
    for user_id in user_ids:
    query = db.session.query(participants.c.thread_id) \
    .filter(participants.c.user_id==user_id)
    queries.append(query)
    thread_ids = [r[0] for r in db.session.query(participants.c.thread_id) \
    .intersect(*queries).all()]
    threads = Thread.query.filter(Thread.id.in_(thread_ids)).all()
    return threads