From b3905e062d612934d07bc44c10d15cd118fe24e3 Mon Sep 17 00:00:00 2001 From: Keith Edmunds Date: Fri, 6 Oct 2023 10:58:15 +0100 Subject: [PATCH] Improve artist search Replicate recent changes in title search to artist search --- app/models.py | 14 ++++++++++++-- app/musicmuster.py | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/app/models.py b/app/models.py index 3df1f69..278706f 100644 --- a/app/models.py +++ b/app/models.py @@ -704,13 +704,23 @@ class Tracks(Base): @classmethod def search_artists(cls, session: scoped_session, text: str) -> List["Tracks"]: - """Search case-insenstively for artists containing str""" + """ + Search case-insenstively for artists containing str + + The query performs an outer join with 'joinedload' to populate the results + from the Playdates table at the same time. unique() needed; see + https://docs.sqlalchemy.org/en/20/orm/queryguide/relationships.html#joined-eager-loading + """ return ( session.execute( - select(cls).where(cls.artist.ilike(f"%{text}%")).order_by(cls.title) + select(cls) + .options(joinedload(Tracks.playdates)) + .where(cls.artist.ilike(f"%{text}%")) + .order_by(cls.title) ) .scalars() + .unique() .all() ) diff --git a/app/musicmuster.py b/app/musicmuster.py index 9427e72..308f92b 100755 --- a/app/musicmuster.py +++ b/app/musicmuster.py @@ -1953,7 +1953,7 @@ class DbDialog(QDialog): if self.ui.radioTitle.isChecked(): matches = Tracks.search_titles(self.session, "%" + s) else: - matches = Tracks.search_artists(self.session, s) + matches = Tracks.search_artists(self.session, "%" + s) if matches: for track in matches: last_played = None