diff --git a/lib/galaxy/model/__init__.py b/lib/galaxy/model/__init__.py index 722afb197ff7..c19ec9770bcf 100644 --- a/lib/galaxy/model/__init__.py +++ b/lib/galaxy/model/__init__.py @@ -10761,7 +10761,9 @@ class CleanupEventImplicitlyConvertedDatasetAssociationAssociation(Base): # See https://github.com/sqlalchemy/sqlalchemy/discussions/7638 for approach session_partition = select( GalaxySession, - func.row_number().over(order_by=GalaxySession.update_time, partition_by=GalaxySession.user_id).label("index"), + func.row_number() + .over(order_by=GalaxySession.update_time.desc(), partition_by=GalaxySession.user_id) + .label("index"), ).alias() partitioned_session = aliased(GalaxySession, session_partition) User.current_galaxy_session = relationship( diff --git a/test/unit/data/test_galaxy_mapping.py b/test/unit/data/test_galaxy_mapping.py index ccda99693847..1f19f0df8600 100644 --- a/test/unit/data/test_galaxy_mapping.py +++ b/test/unit/data/test_galaxy_mapping.py @@ -698,6 +698,17 @@ def _non_empty_flush(self): session.add(lf) session.flush() + def test_current_session(self): + user = model.User(email="testworkflows@bx.psu.edu", password="password") + galaxy_session = model.GalaxySession() + galaxy_session.user = user + self.persist(user, galaxy_session) + assert user.current_galaxy_session == galaxy_session + new_galaxy_session = model.GalaxySession() + new_galaxy_session.user = user + self.persist(user, new_galaxy_session) + assert user.current_galaxy_session == new_galaxy_session + def test_flush_refreshes(self): # Normally I don't believe in unit testing library code, but the behaviors around attribute # states and flushing in SQL Alchemy is very subtle and it is good to have a executable