Fix Unknown Song in search: properly serialize nested relationships

This commit is contained in:
fullsizemalt 2025-12-25 21:52:41 -08:00
parent 8a46000b9d
commit 45608bfdfb

View file

@ -4,7 +4,6 @@ from sqlmodel import Session, select, col
from sqlalchemy.orm import selectinload
from database import get_session
from models import Show, Song, Venue, Tour, Group, Performance, PerformanceNickname, Comment, Review
from schemas import ShowRead, SongRead, VenueRead, TourRead, GroupRead
router = APIRouter(prefix="/search", tags=["search"])
@ -48,21 +47,51 @@ def global_search(
).all()
# Search Nicknames
nicknames = session.exec(
nicknames_raw = session.exec(
select(PerformanceNickname)
.options(selectinload(PerformanceNickname.performance).selectinload(Performance.song))
.options(
selectinload(PerformanceNickname.performance)
.selectinload(Performance.song),
selectinload(PerformanceNickname.performance)
.selectinload(Performance.show)
)
.where(col(PerformanceNickname.nickname).ilike(q_str))
.where(PerformanceNickname.status == "approved")
.limit(limit)
).all()
# Serialize nicknames with nested data
nicknames = []
for n in nicknames_raw:
nicknames.append({
"id": n.id,
"nickname": n.nickname,
"description": n.description,
"performance": {
"id": n.performance.id if n.performance else None,
"song": {"id": n.performance.song.id, "title": n.performance.song.title} if n.performance and n.performance.song else None,
"show": {"slug": n.performance.show.slug, "date": str(n.performance.show.date.date()) if n.performance.show else None} if n.performance and n.performance.show else None
} if n.performance else None
})
# Search Performances by notes
performances = session.exec(
performances_raw = session.exec(
select(Performance)
.options(selectinload(Performance.song), selectinload(Performance.show))
.where(col(Performance.notes).ilike(q_str))
.limit(limit)
).all()
# Serialize performances with nested song/show data
performances = []
for p in performances_raw:
performances.append({
"id": p.id,
"slug": p.slug,
"notes": p.notes,
"song": {"id": p.song.id, "title": p.song.title, "slug": p.song.slug} if p.song else None,
"show": {"slug": p.show.slug, "date": str(p.show.date.date()) if p.show else None} if p.show else None
})
# Search Reviews
reviews = session.exec(