apache/answer · error

user not exist

Error message

user not exist

What it means

Same guard as the activity repo, but in user_active_repo: before recording a user-active/activity record, it locks and fetches the user row by userID and aborts with this error if no row exists. It exists to prevent writing activity data for a nonexistent or deleted user.

Source

Thrown at internal/repo/activity/user_active_repo.go:90

	addActivity := &entity.Activity{
		UserID:           userID,
		ObjectID:         "0",
		OriginalObjectID: "0",
		ActivityType:     cfg.ID,
		Rank:             cfg.GetIntValue(),
		HasRank:          1,
	}

	_, err = ar.data.DB.Transaction(func(session *xorm.Session) (result any, err error) {
		session = session.Context(ctx)

		user := &entity.User{}
		exist, err := session.ID(userID).ForUpdate().Get(user)
		if err != nil {
			return nil, err
		}
		if !exist {
			return nil, fmt.Errorf("user not exist")
		}

		existsActivity := &entity.Activity{}
		exist, err = session.
			And(builder.Eq{"user_id": addActivity.UserID}).
			And(builder.Eq{"activity_type": addActivity.ActivityType}).
			Get(existsActivity)
		if err != nil {
			return nil, err
		}
		if exist {
			return nil, nil
		}

		err = ar.userRankRepo.ChangeUserRank(ctx, session, addActivity.UserID, user.Rank, addActivity.Rank)
		if err != nil {
			return nil, err
		}

View on GitHub (pinned to 3b9f137061)

Solutions

  1. Check the users table for the ID and recreate/restore the user if it was deleted.
  2. Ensure authentication middleware rejects tokens belonging to deleted users before reaching this repo.
  3. Fix any caller passing a zero/empty userID when auth is disabled or in tests.
  4. Add an FK constraint from the activity table's user_id to users.id.

Example fix

// before
if !exist {
    return nil, fmt.Errorf("user not exist")
}
// after
if !exist {
    return nil, fmt.Errorf("user not exist: id=%d", userID)
}
Defensive patterns

Strategy: validation

Validate before calling

if userID == 0 {
    return errors.New("user id is required")
}
var count int64
db.Model(&entity.User{}).Where("id = ?", userID).Count(&count)
if count == 0 {
    return fmt.Errorf("user %d does not exist", userID)
}

Type guard

func isValidUserID(id int64) bool { return id > 0 }

Try / catch

_, err := userActiveRepo.AddActivity(ctx, req)
if err != nil {
    if strings.Contains(err.Error(), "user not exist") {
        return http.StatusNotFound, "account no longer exists"
    }
    return http.StatusInternalServerError, err.Error()
}

Prevention

When it happens

Trigger: Calling the user-active add path with a userID that has no matching row in the users table; the ForUpdate() Get returns exist=false.

Common situations: Requests authenticated with a token for a since-deleted account; test fixtures missing the user row; ID mismatch between services/databases after a migration.

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 apache/answer@3b9f137061 (2026-09-05). Data as JSON: /api/errors/17cc1d8079adbfe2. Report an issue: GitHub.