Skip to content

Instantly share code, notes, and snippets.

@doobeh
Created November 15, 2012 15:34
Show Gist options
  • Select an option

  • Save doobeh/4079214 to your computer and use it in GitHub Desktop.

Select an option

Save doobeh/4079214 to your computer and use it in GitHub Desktop.
Subquery ORM
# Get set of users who anthony sent a message to
## Isolate the user we care about.
anthony = User.query.filter_by(username='anthony').first()
## What messages were sent by that user:
subq = db.session.query(ChatHistory.with_user).filter(ChatHistory.user_id==anthony.id).distinct().subquery()
## What users does that relate to?
users = User.query.filter(User.id.in_(subq)).all()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment