Created
October 8, 2022 13:15
-
-
Save isaacharrisholt/82fa7cdce796b1d8ff2b82fd3c08640c to your computer and use it in GitHub Desktop.
The Python functions for finding users with failed orders
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
from sqlalchemy.orm import Session | |
... | |
class User(Base): | |
... | |
class Order(Base): | |
... | |
def get_users_with_failed_orders_python(session: Session) -> set[User]: | |
"""Get all users with failed orders using a naive Python implementation.""" | |
failed_orders = ( | |
session | |
.query(Order) | |
.prefix_with('SQL_NO_CACHE') | |
.filter(Order.payment_status == False) | |
.all() | |
) | |
users = set() | |
for order in failed_orders: | |
user = ( | |
session | |
.query(User) | |
.prefix_with('SQL_NO_CACHE') | |
.filter(User.id == order.user_id) | |
.one_or_none() | |
) | |
users.add(user) | |
return users | |
def get_users_with_failed_orders_sql(session: Session) -> set[User]: | |
"""Get all users with failed orders using a SQL implementation.""" | |
return set( | |
session | |
.query(User) | |
.prefix_with('SQL_NO_CACHE') | |
.distinct() | |
.join(Order) | |
.filter(Order.payment_status == False) | |
.all() | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment