From 27a4a9eed48b9cd1482729fbd2ba41515ae4f75c Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 27 Apr 2022 18:19:36 -1000 Subject: [PATCH] Adjust get_latest_short_term_statistics query to be postgresql compatible (#70953) --- homeassistant/components/recorder/statistics.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/recorder/statistics.py b/homeassistant/components/recorder/statistics.py index 0056a81fb60..1c993b32bb6 100644 --- a/homeassistant/components/recorder/statistics.py +++ b/homeassistant/components/recorder/statistics.py @@ -1136,16 +1136,20 @@ def get_latest_short_term_statistics( ] most_recent_statistic_row = ( session.query( - StatisticsShortTerm.id, - func.max(StatisticsShortTerm.start), + StatisticsShortTerm.metadata_id, + func.max(StatisticsShortTerm.start).label("start_max"), ) + .filter(StatisticsShortTerm.metadata_id.in_(metadata_ids)) .group_by(StatisticsShortTerm.metadata_id) - .having(StatisticsShortTerm.metadata_id.in_(metadata_ids)) ).subquery() stats = execute( session.query(*QUERY_STATISTICS_SHORT_TERM).join( most_recent_statistic_row, - StatisticsShortTerm.id == most_recent_statistic_row.c.id, + ( + StatisticsShortTerm.metadata_id # pylint: disable=comparison-with-callable + == most_recent_statistic_row.c.metadata_id + ) + & (StatisticsShortTerm.start == most_recent_statistic_row.c.start_max), ) ) if not stats: