From 02785a4ded3961e9b9c6a4862f0b1ae0c757a6b8 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 20 Dec 2024 23:37:16 -1000 Subject: [PATCH] Simplify query to find oldest state (#133700) --- homeassistant/components/recorder/queries.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/homeassistant/components/recorder/queries.py b/homeassistant/components/recorder/queries.py index 8ca7bef2691..34e9ec32f99 100644 --- a/homeassistant/components/recorder/queries.py +++ b/homeassistant/components/recorder/queries.py @@ -640,9 +640,9 @@ def find_states_to_purge( def find_oldest_state() -> StatementLambdaElement: """Find the last_updated_ts of the oldest state.""" return lambda_stmt( - lambda: select(States.last_updated_ts).where( - States.state_id.in_(select(func.min(States.state_id))) - ) + lambda: select(States.last_updated_ts) + .order_by(States.last_updated_ts.asc()) + .limit(1) )