{"record":{"id":"04dcd762624cc080","repo":"AdguardTeam/AdGuardHome","slug":"login-w","errorCode":null,"errorMessage":"login: %w","messagePattern":"login: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/aghuser/db.go","lineNumber":142,"sourceCode":"}\n\n// Create implements the [DB] interface for *DefaultDB.\nfunc (db *DefaultDB) Create(ctx context.Context, u *User) (err error) {\n\tdb.mu.Lock()\n\tdefer db.mu.Unlock()\n\n\tif u.ID == (UserID{}) {\n\t\treturn fmt.Errorf(\"userid: %w\", errors.ErrEmptyValue)\n\t}\n\n\t_, ok := db.userIDToUser[u.ID]\n\tif ok {\n\t\treturn fmt.Errorf(\"userid: %w\", errors.ErrDuplicated)\n\t}\n\n\t_, ok = db.loginToUserID[u.Login]\n\tif ok {\n\t\treturn fmt.Errorf(\"login: %w\", errors.ErrDuplicated)\n\t}\n\n\tdb.userIDToUser[u.ID] = u\n\tdb.loginToUserID[u.Login] = u.ID\n\n\treturn nil\n}\n","sourceCodeStart":124,"sourceCodeEnd":150,"githubUrl":"https://github.com/AdguardTeam/AdGuardHome/blob/b41aefbe51c8dde65e2c50f093996afa0502edf9/internal/aghuser/db.go#L124-L150","documentation":"Create rejected a duplicate login name: another user already occupies u.Login, so the login-to-user mapping cannot be added (wrapped sentinel errors.ErrDuplicated). Logins are unique independent of IDs.","triggerScenarios":"Calling DefaultDB.Create with a login string already present in db.loginToUserID — two accounts with the same username, or a re-run of user provisioning that does not dedupe by login.","commonSituations":"Renaming/creating a user with a username that already exists; retrying a partial import where the first attempt committed some users; case-sensitive collisions ('Alice' vs 'alice' both allowed, but exact matches collide).","solutions":["Check for an existing user by login before creating and update it instead","Choose a different, unique login for the new record","Make import scripts skip-or-update on existing logins"],"exampleFix":"// before\ndb.Create(ctx, &User{ID: id2, Login: \"alice\"}) // alice exists\n// after\ndb.Create(ctx, &User{ID: id2, Login: \"alice2\"})","handlingStrategy":"validation","validationCode":"if _, exists, _ := db.UserByLogin(ctx, u.Login); exists {\n    return fmt.Errorf(\"login %q taken\", u.Login)\n}","typeGuard":null,"tryCatchPattern":"if err := db.Create(ctx, u); err != nil {\n    if errors.Is(err, errors.ErrDuplicated) && strings.HasPrefix(err.Error(), \"login\") { u.Login += \"2\"; err = db.Create(ctx, u) }\n}","preventionTips":["Normalize and dedupe logins during import","Check login availability before presenting create UI/API"],"tags":["user","database","duplicate","login","uniqueness"],"backgroundTag":"duplicate-username","analyzedSha":"b41aefbe51c8dde65e2c50f093996afa0502edf9","analyzedAt":"2026-08-27T04:57:55.097Z","schemaVersion":2},"datasetVersion":"2026-08-27T08:17:20.692Z"}