Skip to content

Commit 43ba76b

Browse files
use sql exists via sqlalchemy any instead of joins for fetching category products
1 parent fa021e6 commit 43ba76b

File tree

1 file changed

+2
-8
lines changed

1 file changed

+2
-8
lines changed

app/migrated_routes/category.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
Product,
1212
Subcategory,
1313
category_subcategory,
14-
subcategory_product,
1514
)
1615
from app.schemas import (
1716
CategoriesOut,
@@ -313,14 +312,9 @@ def get(self, id, page):
313312
abort(404)
314313

315314
products = (
316-
Product.query.join(subcategory_product)
317-
.join(
318-
category_subcategory,
319-
onclause=subcategory_product.c.subcategory_id
320-
== category_subcategory.c.subcategory_id,
315+
Product.query.filter(
316+
Product.subcategories.any(Subcategory.categories.any(id=id))
321317
)
322-
.filter(category_subcategory.c.category_id == id)
323-
.distinct()
324318
.order_by(Product.id.asc())
325319
.paginate(page=page, per_page=CategoryProducts._PER_PAGE, error_out=False)
326320
)

0 commit comments

Comments
 (0)