{"record":{"id":"17cc1d8079adbfe2","repo":"apache/answer","slug":"user-not-exist-17cc1d","errorCode":null,"errorMessage":"user not exist","messagePattern":"user not exist","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/repo/activity/user_active_repo.go","lineNumber":90,"sourceCode":"\taddActivity := &entity.Activity{\n\t\tUserID:           userID,\n\t\tObjectID:         \"0\",\n\t\tOriginalObjectID: \"0\",\n\t\tActivityType:     cfg.ID,\n\t\tRank:             cfg.GetIntValue(),\n\t\tHasRank:          1,\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(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\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\n\t\t}","sourceCodeStart":72,"sourceCodeEnd":108,"githubUrl":"https://github.com/apache/answer/blob/3b9f1370612e690a0b7f230f05e688930db4c6d3/internal/repo/activity/user_active_repo.go#L72-L108","documentation":"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.","triggerScenarios":"Calling the user-active add path with a userID that has no matching row in the users table; the ForUpdate() Get returns exist=false.","commonSituations":"Requests authenticated with a token for a since-deleted account; test fixtures missing the user row; ID mismatch between services/databases after a migration.","solutions":["Check the users table for the ID and recreate/restore the user if it was deleted.","Ensure authentication middleware rejects tokens belonging to deleted users before reaching this repo.","Fix any caller passing a zero/empty userID when auth is disabled or in tests.","Add an FK constraint from the activity table's user_id to users.id."],"exampleFix":"// before\nif !exist {\n    return nil, fmt.Errorf(\"user not exist\")\n}\n// after\nif !exist {\n    return nil, fmt.Errorf(\"user not exist: id=%d\", userID)\n}","handlingStrategy":"validation","validationCode":"if userID == 0 {\n    return errors.New(\"user id is required\")\n}\nvar count int64\ndb.Model(&entity.User{}).Where(\"id = ?\", userID).Count(&count)\nif count == 0 {\n    return fmt.Errorf(\"user %d does not exist\", userID)\n}","typeGuard":"func isValidUserID(id int64) bool { return id > 0 }","tryCatchPattern":"_, err := userActiveRepo.AddActivity(ctx, req)\nif err != nil {\n    if strings.Contains(err.Error(), \"user not exist\") {\n        return http.StatusNotFound, \"account no longer exists\"\n    }\n    return http.StatusInternalServerError, err.Error()\n}","preventionTips":["Reject tokens of deleted users in auth middleware before any repo call.","Validate userID > 0 at the service layer.","Keep user soft-delete in sync so activity lookups still find the row when appropriate.","Seed user fixtures in every test that touches this repo."],"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-14T00:17:10.932Z"}