geektutu/7days-golang · warning

NOT FOUND

Error message

NOT FOUND

What it means

Sentinel error returned by Session.First (within transaction support) when the LIMIT 1 query returns zero rows. It signals that no record satisfies the query conditions; the caller's destination value is not populated.

Source

Thrown at gee-orm/day6-transaction/session/record.go:66

		}
		if err := rows.Scan(values...); err != nil {
			return err
		}
		s.CallMethod(AfterQuery, dest.Addr().Interface())
		destSlice.Set(reflect.Append(destSlice, dest))
	}
	return rows.Close()
}

// First gets the 1st row
func (s *Session) First(value interface{}) error {
	dest := reflect.Indirect(reflect.ValueOf(value))
	destSlice := reflect.New(reflect.SliceOf(dest.Type())).Elem()
	if err := s.Limit(1).Find(destSlice.Addr().Interface()); err != nil {
		return err
	}
	if destSlice.Len() == 0 {
		return errors.New("NOT FOUND")
	}
	dest.Set(destSlice.Index(0))
	return nil
}

// Limit adds limit condition to clause
func (s *Session) Limit(num int) *Session {
	s.clause.Set(clause.LIMIT, num)
	return s
}

// Where adds limit condition to clause
func (s *Session) Where(desc string, args ...interface{}) *Session {
	var vars []interface{}
	s.clause.Set(clause.WHERE, append(append(vars, desc), args...)...)
	return s
}

View on GitHub (pinned to cf36443821)

Solutions

  1. Perform reads inside the same transaction/session that writes the row
  2. Check err.Error()=="NOT FOUND" and handle as empty result; roll back or create as appropriate
  3. Verify isolation level if cross-transaction visibility was expected
  4. Confirm condition arguments and table name

Example fix

// before
return s.Transaction(func() error { return s2.First(&u, "id=?", id) }) // different session
// after
return s.Transaction(func() error { return s.First(&u, "id=?", id) }) // same tx session
Defensive patterns

Strategy: try-catch

Validate before calling

if rows, err := tx.QueryContext(ctx, "SELECT 1 FROM users LIMIT 1"); err == nil { defer rows.Close() } // row visibility check inside tx

Try / catch

err := s.Transaction(func() error {
    if err := s.First(&u, "id = ?", id); err != nil {
        if err.Error() == "NOT FOUND" { return ErrNotFound }
        return err
    }
    return nil
})

Prevention

When it happens

Trigger: First() within s.Transaction(...) where the SELECT LIMIT 1 matches nothing; reading a row created outside the current transaction's isolation; querying an uncommitted row from another connection.

Common situations: Transaction inserts in one session but First runs on another uncommitted session; row deleted earlier in the same transaction; wrong condition values.

Understand the failure class

Background: Record Not Found Errors: "not found", RecordNotFound, and "was not found" — what they mean and how to fix them — this error's family across 28 libraries.

Related errors


AI-assisted analysis of geektutu/7days-golang@cf36443821 (2026-09-03). Data as JSON: /api/errors/5399fcf14909c032. Report an issue: GitHub.