diff --git a/discovery-provider/src/queries/get_save_tracks.py b/discovery-provider/src/queries/get_save_tracks.py index ae28a2a8f8f..7b8753075ea 100644 --- a/discovery-provider/src/queries/get_save_tracks.py +++ b/discovery-provider/src/queries/get_save_tracks.py @@ -1,6 +1,7 @@ from typing import Optional, TypedDict -from sqlalchemy import asc, desc, or_ +from sqlalchemy import asc, desc, func, or_ +from sqlalchemy.sql.functions import coalesce from src.models.social.aggregate_plays import AggregatePlay from src.models.social.save import Save, SaveType from src.models.tracks.aggregate_track import AggregateTrack @@ -82,9 +83,21 @@ def get_save_tracks(args: GetSaveTracksArgs): if sort_method == SortMethod.title: base_query = base_query.order_by(sort_fn(TrackWithAggregates.title)) elif sort_method == SortMethod.artist_name: - base_query = base_query.order_by(sort_fn(TrackWithAggregates.user.name)) + base_query = base_query.join( + TrackWithAggregates.user, aliased=True + ).order_by(sort_fn(TrackWithAggregates.user.name)) elif sort_method == SortMethod.release_date: - base_query = base_query.order_by(sort_fn(TrackWithAggregates.release_date)) + base_query = base_query.order_by( + sort_fn( + coalesce( + func.to_date_safe( + TrackWithAggregates.release_date, + "Dy Mon DD YYYY HH24:MI:SS", + ), + TrackWithAggregates.created_at, + ) + ) + ) elif sort_method == SortMethod.added_date: base_query = base_query.order_by( sort_fn(Save.created_at), desc(TrackWithAggregates.track_id) diff --git a/discovery-provider/src/queries/get_tracks.py b/discovery-provider/src/queries/get_tracks.py index 8cc35ec7997..f179e7340bc 100644 --- a/discovery-provider/src/queries/get_tracks.py +++ b/discovery-provider/src/queries/get_tracks.py @@ -133,7 +133,9 @@ def _get_tracks(session, args): if sort_method == SortMethod.title: base_query = base_query.order_by(sort_fn(TrackWithAggregates.title)) elif sort_method == SortMethod.artist_name: - base_query = base_query.order_by(sort_fn(TrackWithAggregates.user.name)) + base_query = base_query.join( + TrackWithAggregates.user, aliased=True + ).order_by(sort_fn(TrackWithAggregates.user.name)) elif sort_method == SortMethod.release_date: base_query = base_query.order_by( sort_fn( diff --git a/discovery-provider/src/queries/get_user_listening_history.py b/discovery-provider/src/queries/get_user_listening_history.py index 07c01976778..6fe525d11fc 100644 --- a/discovery-provider/src/queries/get_user_listening_history.py +++ b/discovery-provider/src/queries/get_user_listening_history.py @@ -103,7 +103,9 @@ def _get_user_listening_history(session: Session, args: GetUserListeningHistoryA if sort_method == SortMethod.title: base_query = base_query.order_by(sort_fn(TrackWithAggregates.title)) elif sort_method == SortMethod.artist_name: - base_query = base_query.order_by(sort_fn(TrackWithAggregates.user.name)) + base_query = base_query.join(TrackWithAggregates.user, aliased=True).order_by( + sort_fn(TrackWithAggregates.user.name) + ) elif sort_method == SortMethod.release_date: base_query = base_query.order_by( sort_fn(