Skip to content

Instantly share code, notes, and snippets.

@inklesspen
Created July 15, 2018 00:36
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 inklesspen/e562915bf4af2eebd1a6e8d3319579d5 to your computer and use it in GitHub Desktop.
Save inklesspen/e562915bf4af2eebd1a6e8d3319579d5 to your computer and use it in GitHub Desktop.
# Assume these classes are fully defined, including relationships
class User(Base):
pass
class Order(Base):
pass
# Requested: SELECT DISTINCT user.id FROM user, order WHERE user.id = order.user_id
session.query(User.id).select_from(User, Order).filter(User.id == Order.user_id).distinct()
# Produces: SELECT DISTINCT user.id AS user_id FROM user, order WHERE user.id = order.user_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment