diff --git a/lib/providers/nutrition_repository.dart b/lib/providers/nutrition_repository.dart index 76eed816a..57d289d86 100644 --- a/lib/providers/nutrition_repository.dart +++ b/lib/providers/nutrition_repository.dart @@ -252,7 +252,7 @@ class NutritionRepository { Stream> watchAllLogsHydrated() { _logger.finer('Watching all local diary entries'); final query = _db.select(_db.logItemTable).join([ - innerJoin( + leftOuterJoin( _db.ingredientTable, _db.ingredientTable.id.equalsExp(_db.logItemTable.ingredientId), ), @@ -277,18 +277,20 @@ class NutritionRepository { for (final row in rows) { final log = row.readTable(_db.logItemTable); - final ingredient = row.readTable(_db.ingredientTable); + final ingredient = row.readTableOrNull(_db.ingredientTable); final image = row.readTableOrNull(_db.ingredientImageTable); final wu = row.readTableOrNull(_db.ingredientWeightUnitTable); - final ingEntry = ingredients.putIfAbsent(ingredient.id, () => ingredient); - if (image != null) { - ingEntry.image = image; - } - if (wu != null) { - final list = weightUnitsByIngredient.putIfAbsent(ingredient.id, () => []); - if (!list.any((w) => w.id == wu.id)) { - list.add(wu); + if (ingredient != null) { + final ingEntry = ingredients.putIfAbsent(ingredient.id, () => ingredient); + if (image != null) { + ingEntry.image = image; + } + if (wu != null) { + final list = weightUnitsByIngredient.putIfAbsent(ingredient.id, () => []); + if (!list.any((w) => w.id == wu.id)) { + list.add(wu); + } } } diff --git a/test/nutrition/nutrition_repository_test.dart b/test/nutrition/nutrition_repository_test.dart index d68ad618c..57bb1128a 100644 --- a/test/nutrition/nutrition_repository_test.dart +++ b/test/nutrition/nutrition_repository_test.dart @@ -443,6 +443,27 @@ void main() { expect(emitted.single.ingredient?.name, 'Apple'); }); + test('log whose ingredient has not synced yet still emits with null', () async { + // Models the window between addLogLocalDrift and PowerSync pulling the + // referenced ingredient down. The diary entry must stay visible (else the + // daily totals undercount) rather than being dropped by the join. + await seedPlan(id: planUuid1); + await repo.addLogLocalDrift( + LogItem( + planId: planUuid1, + ingredientId: 42, + amount: 100, + datetime: DateTime.utc(2026, 4, 15), + ), + ); + + final emitted = await repo.watchAllLogsHydrated().first; + + expect(emitted, hasLength(1)); + expect(emitted.single.ingredient, isNull); + expect(emitted.single.amount, 100); + }); + test('watchAllLogsHydrated attaches image and weight units to each ingredient', () async { await seedPlan(id: planUuid1); await seedIngredient(id: 1, name: 'Apple');