fix: search endpoint — correct column name and DISTINCT/ORDER BY

- 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) <noreply@anthropic.com>
This commit is contained in:
2026-04-10 10:55:55 +02:00
parent aba061dd43
commit 5f3fa5240e

View File

@@ -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: