{"record":{"id":"19758d6738e9e377","repo":"apache/answer","slug":"user-not-exist","errorCode":null,"errorMessage":"user not exist","messagePattern":"user not exist","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/repo/activity/review_repo.go","lineNumber":94,"sourceCode":"\t\tTriggerUserID:    converter.StringToInt64(act.TriggerUserID),\n\t\tObjectID:         act.ObjectID,\n\t\tOriginalObjectID: act.OriginalObjectID,\n\t\tActivityType:     cfg.ID,\n\t\tRank:             cfg.GetIntValue(),\n\t\tHasRank:          1,\n\t\tRevisionID:       converter.StringToInt64(act.RevisionID),\n\t}\n\n\t_, err = ar.data.DB.Transaction(func(session *xorm.Session) (result any, err error) {\n\t\tsession = session.Context(ctx)\n\n\t\tuser := &entity.User{}\n\t\texist, err := session.ID(addActivity.UserID).ForUpdate().Get(user)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tif !exist {\n\t\t\treturn nil, fmt.Errorf(\"user not exist\")\n\t\t}\n\n\t\texistsActivity := &entity.Activity{}\n\t\texist, err = session.\n\t\t\tAnd(builder.Eq{\"user_id\": addActivity.UserID}).\n\t\t\tAnd(builder.Eq{\"activity_type\": addActivity.ActivityType}).\n\t\t\tAnd(builder.Eq{\"revision_id\": addActivity.RevisionID}).\n\t\t\tGet(existsActivity)\n\t\tif err != nil {\n\t\t\treturn nil, err\n\t\t}\n\t\tif exist {\n\t\t\treturn nil, nil\n\t\t}\n\n\t\terr = ar.userRankRepo.ChangeUserRank(ctx, session, addActivity.UserID, user.Rank, addActivity.Rank)\n\t\tif err != nil {\n\t\t\treturn nil, err","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/apache/answer/blob/3b9f1370612e690a0b7f230f05e688930db4c6d3/internal/repo/activity/review_repo.go#L76-L112","documentation":"This error is thrown by the activity repository inside a transaction when the user referenced by addActivity.UserID cannot be found in the users table. The lookup uses a row-locking (ForUpdate) select, so it also doubles as a consistency guard: if the row is missing, the whole AddActivity transaction is aborted with this error instead of inserting an activity for a dangling user.","triggerScenarios":"Calling the activity-add service/repo path with a UserID that does not exist in the users table (deleted user, wrong ID, ID from another environment's database).","commonSituations":"Seeding or restoring a database where users were purged but activities reference their IDs; passing an unparsed or default (0) user ID from an unauthenticated or miswired context; stale external references after account deletion.","solutions":["Verify the user ID being passed actually exists: SELECT id FROM users WHERE id = <id>;","Fix upstream callers so the authenticated/logged-in user's real ID is used instead of a zero value.","Re-seed or restore the users table if data was lost, or remove orphaned activity requests.","Add a foreign key on activities.user_id so this is caught at insert time with a clearer DB error."],"exampleFix":"// before\nexist, err := session.ID(addActivity.UserID).ForUpdate().Get(user)\nif !exist {\n    return nil, fmt.Errorf(\"user not exist\")\n}\n// after\nexist, err := session.ID(addActivity.UserID).ForUpdate().Get(user)\nif err != nil {\n    return nil, err\n}\nif !exist {\n    return nil, fmt.Errorf(\"user not exist: id=%d\", addActivity.UserID)\n}","handlingStrategy":"validation","validationCode":"var userCount int64\nerr := db.Model(&entity.User{}).Where(\"id = ?\", addActivity.UserID).Count(&userCount)\nif err == nil && userCount == 0 {\n    return fmt.Errorf(\"cannot add activity: user %d does not exist\", addActivity.UserID)\n}","typeGuard":"func userExists(id int64, users []entity.User) bool {\n    for _, u := range users {\n        if u.ID == id {\n            return true\n        }\n    }\n    return false\n}","tryCatchPattern":"activity, err := activityRepo.AddActivity(ctx, req)\nif err != nil {\n    if strings.Contains(err.Error(), \"user not exist\") {\n        return http.StatusNotFound, \"user does not exist\"\n    }\n    return http.StatusInternalServerError, err.Error()\n}","preventionTips":["Always use the authenticated user's ID from context, never a client-supplied value.","Add a foreign key on activities.user_id referencing users.id.","Purge dependent activity rows when deleting users (cascade or soft-delete).","In tests, always seed the user before exercising activity APIs."],"tags":["database","user-lookup","referential-integrity"],"backgroundTag":"record-not-found","analyzedSha":"3b9f1370612e690a0b7f230f05e688930db4c6d3","analyzedAt":"2026-09-05T18:18:39.533Z","contentChangedAt":"2026-09-05T18:18:39.533Z","schemaVersion":2},"datasetVersion":"2026-09-12T22:17:10.623Z"}