diff --git a/backend/endpoints/roms/__init__.py b/backend/endpoints/roms/__init__.py index bb62e9b7c..99efad7ec 100644 --- a/backend/endpoints/roms/__init__.py +++ b/backend/endpoints/roms/__init__.py @@ -580,19 +580,26 @@ def get_roms( def _transform(items: Sequence[Rom]) -> list[SimpleRomSchema]: rom_ids = [i.id for i in items] - sibling_ids_by_rom = db_rom_handler.get_sibling_ids_for_roms( - rom_ids, session=session - ) files_by_rom = ( db_rom_handler.get_files_for_roms(rom_ids, session=session) if with_files else {} ) - siblings_by_rom = ( - db_rom_handler.get_siblings_for_roms(rom_ids, session=session) - if with_siblings - else {} - ) + + if with_siblings: + siblings_by_rom = db_rom_handler.get_siblings_for_roms( + rom_ids, session=session + ) + # Derive the id list from the full siblings instead of a second query. + sibling_ids_by_rom = { + rid: sorted({s.id for s in sibs}) + for rid, sibs in siblings_by_rom.items() + } + else: + siblings_by_rom = {} + sibling_ids_by_rom = db_rom_handler.get_sibling_ids_for_roms( + rom_ids, session=session + ) return [ SimpleRomSchema.from_orm_with_request( diff --git a/backend/tests/endpoints/roms/test_rom.py b/backend/tests/endpoints/roms/test_rom.py index b8ceedb57..5e611f275 100644 --- a/backend/tests/endpoints/roms/test_rom.py +++ b/backend/tests/endpoints/roms/test_rom.py @@ -147,6 +147,7 @@ def test_get_all_roms_with_siblings( rom_a, rom_b = siblings assert [s["id"] for s in items[rom_a.id]["siblings"]] == [rom_b.id] assert [s["id"] for s in items[rom_b.id]["siblings"]] == [rom_a.id] + assert items[rom_a.id]["sibling_ids"] == [rom_b.id] # with_siblings alone must not pull in files. assert items[rom_a.id]["files"] == []