Skip to content

Commit

Permalink
Optimize query with sqlalchemy joinedload
Browse files Browse the repository at this point in the history
  • Loading branch information
raphodn committed Dec 18, 2023
1 parent 75f8af5 commit 2b055ca
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions app/crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from fastapi import UploadFile
from sqlalchemy import select
from sqlalchemy.orm import Session
from sqlalchemy.orm import Session, joinedload
from sqlalchemy.sql import func

from app import config
Expand Down Expand Up @@ -105,9 +105,15 @@ def update_product(db: Session, product: ProductBase, update_dict: dict):

# Prices
# ------------------------------------------------------------------------------
def get_prices_query(filters: PriceFilter | None = None):
def get_prices_query(
with_join_product=True, with_join_location=True, filters: PriceFilter | None = None
):
"""Useful for pagination."""
query = select(Price)
if with_join_product:
query = query.options(joinedload(Price.product))
if with_join_location:
query = query.options(joinedload(Price.location))
if filters:
query = filters.filter(query)
return query
Expand Down

0 comments on commit 2b055ca

Please sign in to comment.