Skip to content

Instantly share code, notes, and snippets.

@alexanderjulo
Created December 4, 2012 14:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexanderjulo/4204824 to your computer and use it in GitHub Desktop.
Save alexanderjulo/4204824 to your computer and use it in GitHub Desktop.
find common elements in in a many-to-many helper table
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