geektutu/7days-golang · warning

NOT FOUND

Error message

NOT FOUND

What it means

Sentinel error returned by Session.First when the query with LIMIT 1 matches no rows: destSlice has length 0, so there is no record to assign to the destination value. It indicates the requested first row does not exist, not a query failure.

Source

Thrown at gee-orm/day4-chain-operation/session/record.go:63

			values = append(values, dest.FieldByName(name).Addr().Interface())
		}
		if err := rows.Scan(values...); err != nil {
			return err
		}
		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. Check err with errors.Is/strings equality against "NOT FOUND" and treat as an expected empty result, not a failure
  2. Verify the WHERE condition values and that records were inserted before the lookup
  3. Confirm the struct's table name matches where the data was written
  4. Prefer inserting/upserting the record if the miss is unexpected in the workflow

Example fix

// before
var u User
if err := s.First(&u, "id = ?", id); err != nil {
    return err // crashes on legit misses
}
// after
var u User
if err := s.First(&u, "id = ?", id); err != nil {
    if err.Error() == "NOT FOUND" {
        return ErrUserNotFound // domain-level handling
    }
    return err
}
Defensive patterns

Strategy: try-catch

Validate before calling

var count int64
_ = s.Model(&User{}).Count(&count) // confirm table has rows before First

Try / catch

var u User
if err := s.First(&u, "name = ?", name); err != nil {
    if err.Error() == "NOT FOUND" {
        // empty-result path: create default or return 404
        return handleMissing(name)
    }
    return err // real query failure
}

Prevention

When it happens

Trigger: Calling session.First(&User{}, "name = ?", "bob") where no row with name='bob' exists; querying by a wrong primary key; querying a table before any record was inserted.

Common situations: Test fixtures not seeded before First(); string/number type mismatch in the WHERE clause (e.g. '1' vs 1); querying against the wrong table name derived from the struct; looking up a deleted record.

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/291d580b6b6992e5. Report an issue: GitHub.