geektutu/7days-golang · warning

NOT FOUND

Error message

NOT FOUND

What it means

Sentinel error returned by Session.First (in the migrate-enabled session) when the LIMIT 1 query finds no rows. It is a not-found signal, distinct from SQL or scan errors, and the destination value stays untouched.

Source

Thrown at gee-orm/day7-migrate/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. Seed data after AutoMigrate before querying
  2. Check the table name/columns still match the struct after migrations
  3. Handle the sentinel as empty-result and create the record if needed
  4. Wrap into a typed not-found error for callers

Example fix

// before
engine.AutoMigrate(&User{})
s.First(&u) // empty table -> NOT FOUND
// after
engine.AutoMigrate(&User{})
s.Insert(&User{Name: "demo"})
s.First(&u)
Defensive patterns

Strategy: try-catch

Validate before calling

has, _ := s.Model(&User{}).Count(&count); if count == 0 { seed() } // post-migrate seeding check

Try / catch

if err := s.First(&u); err != nil {
    if err.Error() == "NOT FOUND" { return seedAndRetry() }
    return err
}

Prevention

When it happens

Trigger: First() after migrating a fresh schema (tables exist but are empty); querying rows against a renamed table after a migration changed the naming; stale conditions referencing dropped columns.

Common situations: Running AutoMigrate on a fresh DB then immediately reading expecting seeded data; migration renamed the table so old queries hit an empty legacy table.

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/0f6ef67ad0fa06f8. Report an issue: GitHub.