@@ -189,10 +189,25 @@ async def list(
189189 count_stmt = select (func .count ()).select_from (base_stmt .alias ())
190190 total_count = (await session .execute (count_stmt )).scalar_one ()
191191
192- stmt = base_stmt .order_by (
193- timestamp_col .desc ().nulls_last (),
194- self .task_model .id .desc (),
195- )
192+ # Get paginated results
193+ # Some dialects (e.g. MySQL/MariaDB) do not support the SQL 'NULLS LAST' syntax.
194+ # Emulate NULLS LAST for descending timestamp order when needed by ordering
195+ # on the NULL-ness first, then the value descending.
196+ bind = session .get_bind ()
197+ dialect_name = getattr (getattr (bind , 'dialect' , None ), 'name' , '' )
198+ dialect_name = dialect_name .lower () if dialect_name else ''
199+ if dialect_name in ('mysql' , 'mariadb' ):
200+ # Put non-NULL timestamps first (is_(None()) yields 0 for non-null, 1 for null)
201+ stmt = base_stmt .order_by (
202+ timestamp_col .is_ (None ()),
203+ timestamp_col .desc (),
204+ self .task_model .id .desc (),
205+ )
206+ else :
207+ stmt = base_stmt .order_by (
208+ timestamp_col .desc ().nulls_last (),
209+ self .task_model .id .desc (),
210+ )
196211
197212 # Get paginated results
198213 if params .page_token :
0 commit comments