Fix a few cartesian products in recorder tests (#87106)

Fixes
```
recorder/test_init.py:251: SAWarning: SELECT statement has a cartesian product between FROM element(s) "states" and FROM element "state_attributes".  Apply join condition(s) between each element to resolve.
```
This commit is contained in:
J. Nick Koston 2023-02-02 00:47:09 -06:00 committed by GitHub
parent 29d6ae8d0c
commit 1fd58b6cb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 6 deletions

View File

@ -5,7 +5,7 @@ from sqlalchemy import select
from sqlalchemy.engine.row import Row
from homeassistant.components.recorder import get_instance
from homeassistant.components.recorder.db_schema import EventData, States
from homeassistant.components.recorder.db_schema import EventData, Events, States
from homeassistant.components.recorder.filters import (
Filters,
extract_include_exclude_filter_conf,
@ -54,9 +54,9 @@ async def _async_get_states_and_events_with_filter(
def _get_events_with_session():
with session_scope(hass=hass) as session:
return session.execute(
select(EventData.shared_data).filter(
sqlalchemy_filter.events_entity_filter()
)
select(EventData.shared_data)
.outerjoin(Events, EventData.data_id == Events.data_id)
.filter(sqlalchemy_filter.events_entity_filter())
).all()
filtered_events_entity_ids = set()

View File

@ -214,7 +214,11 @@ async def test_saving_state(recorder_mock, hass: HomeAssistant):
with session_scope(hass=hass) as session:
db_states = []
for db_state, db_state_attributes in session.query(States, StateAttributes):
for db_state, db_state_attributes in session.query(
States, StateAttributes
).outerjoin(
StateAttributes, States.attributes_id == StateAttributes.attributes_id
):
db_states.append(db_state)
state = db_state.to_native()
state.attributes = db_state_attributes.to_native()
@ -248,7 +252,11 @@ async def test_saving_state_with_nul(
with session_scope(hass=hass) as session:
db_states = []
for db_state, db_state_attributes in session.query(States, StateAttributes):
for db_state, db_state_attributes in session.query(
States, StateAttributes
).outerjoin(
StateAttributes, States.attributes_id == StateAttributes.attributes_id
):
db_states.append(db_state)
state = db_state.to_native()
state.attributes = db_state_attributes.to_native()