Skip to content

Conversation

@ajaynz
Copy link
Collaborator

@ajaynz ajaynz commented Nov 17, 2025

Fixes #35

Problem

Duplicate items were showing when the Fastest Delivery sort option was selected. This occurred because the SQL query joins with the ProductDeliveryLink table, creating duplicate rows when a product has multiple delivery options.

Solution

Added .distinct() to all delivery_fastest sorting queries to ensure each product appears exactly once in the results, regardless of how many delivery options it has.

Testing

✅ All 51 backend tests pass
✅ All 34 E2E tests pass
✅ All CI checks pass
✅ Tested with and without category filters
✅ Tested with and without delivery option filters

Changes

Modified /api/products endpoint in backend/app/main.py:

  • Added .distinct() when delivery filter is applied with express speed
  • Added .distinct() when delivery filter is applied with other speeds
  • Added .distinct() when no delivery filter is applied

This ensures no duplicate products appear in any Fastest Delivery sorting scenario.

When sorting by 'Fastest Delivery', products with multiple delivery
options were appearing as duplicates due to SQL JOIN with
ProductDeliveryLink table creating multiple rows.

Added .distinct() to all delivery_fastest sorting queries to ensure
each product appears exactly once in results.

- Fixed duplicate items when no delivery filter selected
- Fixed duplicate items when delivery option filter applied
- All 51 backend tests pass
- All 34 E2E tests pass
Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary: This diff adds .distinct() calls to multiple query branches in the product API endpoint to eliminate duplicate results.

Key concerns:

  • The diff shows .distinct() being added to only some conditional branches. Without seeing the complete file, I cannot confirm if all branches are consistently handled, but the visible price_asc branch at line 261 lacks the .distinct() call while others have it.
  • The .distinct() placement after .order_by() is unusual and may cause database-specific issues. Standard practice is to apply DISTINCT before ordering.

These inconsistencies could lead to different behavior depending on which sorting option the user selects, which is likely unintended.

View this review on Amp

).asc(),
cast(ColumnElement[float], Product.price).asc(),
)
.distinct()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .distinct() call is placed after .order_by(). In SQLAlchemy (and SQL in general), DISTINCT operations should typically be applied before ordering to ensure correct behavior across different database backends. This order may cause issues with PostgreSQL's DISTINCT ON or when the ORDER BY columns aren't in the SELECT list. Consider moving .distinct() before the .order_by() call.

.distinct()
)
elif sort == "price_asc":
stmt = stmt.order_by(cast(ColumnElement[float], Product.price).asc())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent application of .distinct(): The price_asc sorting branch doesn't include .distinct() while other sorting branches do. This inconsistency suggests either a missing .distinct() call here (if deduplication is required for all queries) or unnecessary .distinct() calls in other branches (if deduplication isn't needed). This should be made consistent across all branches.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Duplicate items showing when Fastest Delivery sort chosen

2 participants