Skip to content

Conversation

@tnederlof
Copy link
Collaborator

@tnederlof tnederlof commented Nov 4, 2025

Fixes #35

Issue

Duplicate items were showing when "Fastest Delivery" sort option was selected. This occurred regardless of whether a category filter was also applied.

Root Cause

The backend API endpoint joins and tables for sorting purposes when sort=delivery_fastest. However, products with multiple delivery options create cartesian products from these joins, resulting in duplicate products in the query results.

Solution

Added .distinct() to all three code paths in the delivery_fastest sorting logic to ensure each product appears only once in the results, regardless of how many delivery options it has.

Testing

  • ✅ All 51 backend tests pass
  • ✅ All 34 E2E tests pass
  • ✅ Full CI pipeline passes (backend checks, frontend lint, build, E2E tests)

Changes

  • Modified backend/app/main.py to add .distinct() after sorting operations in the delivery_fastest sort paths

Amp-Thread-ID: https://ampcode.com/threads/T-1c9167c1-0cbc-4b9b-b484-7c6e780838a3

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 query chains in the get_products_api function to eliminate duplicate product results. The changes affect the fast_delivery sorting logic (both ascending and descending branches).

Issue Found

The main issue is inconsistent application of the .distinct() modifier. While it's added to the fast_delivery sorting branches, the price_asc sorting branch at line 257 doesn't receive the same treatment. This inconsistency suggests either an oversight or that the duplication issue only affects certain query paths, which would benefit from clarification.

If duplicate products are a concern that warranted adding .distinct() to some branches, the same logic should likely apply to all sorting options unless there's a specific reason for the difference.

View this review on Amp

.order_by(
cast(ColumnElement[int], DeliveryOption.estimated_days_min).asc(),
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(): This sorting branch doesn't have .distinct() applied while all the fast_delivery sorting branches above do. If the goal is to eliminate duplicate products in the results, this branch should also include .distinct() after the order_by clause for consistency.

Fixes #35 - Products were appearing multiple times when sorted by
Fastest Delivery due to JOIN with ProductDeliveryLink creating
duplicate rows for products with multiple delivery options.

Added .distinct() to all delivery_fastest sorting queries to ensure
each product appears only once in the results.
@ajaynz ajaynz force-pushed the fix/duplicate-items-fastest-delivery branch from 6b3508f to 421a2ec Compare November 11, 2025 08:35
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

3 participants