From 5f3fa5240e4da4ed1533c9a85ab36591aaa26ad1 Mon Sep 17 00:00:00 2001 From: dtoro Date: Fri, 10 Apr 2026 10:55:55 +0200 Subject: [PATCH] =?UTF-8?q?fix:=20search=20endpoint=20=E2=80=94=20correct?= =?UTF-8?q?=20column=20name=20and=20DISTINCT/ORDER=20BY?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Fix Photo.created_at → Photo.added_at (column doesn't exist) - Fix Postgres DISTINCT + ORDER BY conflict by using a subquery for tag_id filtering instead of JOIN + DISTINCT on the outer query Co-Authored-By: Claude Opus 4.6 (1M context) --- backend/app/services/search.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/backend/app/services/search.py b/backend/app/services/search.py index 406ecf1..caf4f5a 100644 --- a/backend/app/services/search.py +++ b/backend/app/services/search.py @@ -102,12 +102,16 @@ async def hybrid_search( # If no text query, fall back to recent photos if not q: - stmt = select(Photo.id).order_by(Photo.created_at.desc()) if tag_ids: from app.models.tags import photo_tags - stmt = stmt.join(photo_tags, Photo.id == photo_tags.c.photo_id).where( + # Subquery to get distinct photo_ids matching the tag filter + sub = select(photo_tags.c.photo_id).where( photo_tags.c.tag_id.in_(tag_ids) - ).distinct() + ).distinct().subquery() + stmt = select(Photo.id).join(sub, Photo.id == sub.c.photo_id) + else: + stmt = select(Photo.id) + stmt = stmt.order_by(Photo.added_at.desc()) if date_from: stmt = stmt.where(Photo.taken_at >= date_from) if date_to: