diff --git a/table.go b/table.go index e0d591e..3e96fd5 100644 --- a/table.go +++ b/table.go @@ -417,10 +417,8 @@ func (t *Table[T, P, I]) Iter(ctx context.Context, where sq.Sqlizer, orderBy []s defer rows.Close() for rows.Next() { var record T - if err := t.Query.Scan.ScanOne(&record, rows); err != nil { - if !errors.Is(err, pgx.ErrNoRows) { - yield(nil, err) - } + if err := t.Query.Scan.ScanRow(&record, rows); err != nil { + yield(nil, err) return } if !yield(&record, nil) { diff --git a/tests/table_test.go b/tests/table_test.go index 52f0a77..b3356a6 100644 --- a/tests/table_test.go +++ b/tests/table_test.go @@ -619,6 +619,34 @@ func TestHardDeleteByID(t *testing.T) { }) } +func TestIter(t *testing.T) { + truncateAllTables(t) + + ctx := t.Context() + db := initDB(DB) + + account := &Account{Name: "Iter Account"} + err := db.Accounts.Insert(ctx, account) + require.NoError(t, err) + + const total = 100 + for i := range total { + err := db.Articles.Insert(ctx, &Article{AccountID: account.ID, Author: fmt.Sprintf("Author %03d", i)}) + require.NoError(t, err) + } + + iter, err := db.Articles.Iter(ctx, sq.Eq{"account_id": account.ID}, []string{"id"}) + require.NoError(t, err) + + var count int + for article, err := range iter { + require.NoError(t, err) + require.NotNil(t, article) + count++ + } + require.Equal(t, total, count, "Iter should yield all rows") +} + func TestLockForUpdates(t *testing.T) { truncateAllTables(t)