Created
December 4, 2012 14:59
-
-
Save alexanderjulo/4204824 to your computer and use it in GitHub Desktop.
find common elements in in a many-to-many helper table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment