{"record":{"id":"09e3ffce0a78c364","repo":"mlflow/mlflow","slug":"resource-already-exists-09e3ff","errorCode":"RESOURCE_ALREADY_EXISTS","errorMessage":"User (username={username}) already exists. Error: {e}","messagePattern":"User \\(username=(.+?)\\) already exists\\. Error: (.+?)","errorType":"exception","errorClass":"MlflowException","httpStatus":409,"severity":"error","filePath":"mlflow/server/auth/sqlalchemy_store.py","lineNumber":157,"sourceCode":"        with self.ManagedSessionMaker() as session:\n            try:\n                user = self._get_user(session, username)\n                return check_password_hash(user.password_hash, password)\n            except MlflowException:\n                return False\n\n    def create_user(self, username: str, password: str, is_admin: bool = False) -> User:\n        _validate_username(username)\n        _validate_password(password)\n        pwhash = generate_password_hash(password)\n        with self.ManagedSessionMaker(read_only=False) as session:\n            try:\n                user = SqlUser(username=username, password_hash=pwhash, is_admin=is_admin)\n                session.add(user)\n                session.flush()\n                return user.to_mlflow_entity()\n            except IntegrityError as e:\n                raise MlflowException(\n                    f\"User (username={username}) already exists. Error: {e}\",\n                    RESOURCE_ALREADY_EXISTS,\n                ) from e\n\n    @staticmethod\n    def _get_user(session, username: str) -> SqlUser:\n        try:\n            return session.query(SqlUser).filter(SqlUser.username == username).one()\n        except NoResultFound:\n            raise MlflowException(\n                f\"User with username={username} not found\",\n                RESOURCE_DOES_NOT_EXIST,\n            )\n        except MultipleResultsFound:\n            raise MlflowException(\n                f\"Found multiple users with username={username}\",\n                INVALID_STATE,\n            )","sourceCodeStart":139,"sourceCodeEnd":175,"githubUrl":"https://github.com/mlflow/mlflow/blob/6a27f2decc0b76eb1b54af31849784addb357dbc/mlflow/server/auth/sqlalchemy_store.py#L139-L175","documentation":"create_user inserts a SqlUser and lets the database enforce username uniqueness via a unique constraint; on an IntegrityError (duplicate username), it re-raises as MlflowException with RESOURCE_ALREADY_EXISTS. This is the standard pre-check-free pattern relying on DB constraints.","triggerScenarios":"Calling create_user (or the REST /api/2.0/mlflow/users/create endpoint, or the initial admin-creation bootstrap) with a username that already exists in the users table.","commonSituations":"Re-running auth bootstrap scripts (e.g. MLFLOW_AUTH_CONFIG_PATH init creating the default admin) against an already-initialized DB; concurrent user creation; duplicate signup attempts.","solutions":["Use a different username, or query the existing user via get_user/auth APIs and update instead of create","Catch MlflowException with code RESOURCE_ALREADY_EXISTS and treat it as idempotent in bootstrap scripts","Check for the user's existence before calling create_user"],"exampleFix":"// before\nstore.create_user(\"admin@example.com\", password, True)\n// after\ntry:\n    store.create_user(\"admin@example.com\", password, True)\nexcept MlflowException as e:\n    if e.get_http_status_code() != 409:\n        raise","handlingStrategy":"try-catch","validationCode":"existing = store.get_user(username)\nif existing:\n    user = existing\nelse:\n    user = store.create_user(username, password, is_admin)","typeGuard":null,"tryCatchPattern":"from mlflow.exceptions import MlflowException, RESOURCE_ALREADY_EXISTS\ntry:\n    user = store.create_user(username, password, is_admin)\nexcept MlflowException as e:\n    if e.get_http_status_code() == 409 or RESOURCE_ALREADY_EXISTS in str(e.code):\n        user = store.get_user(username)\n    else:\n        raise","preventionTips":["Make user-creation bootstrap scripts idempotent (check-then-create or catch 409)","Run bootstrap only once per database","Uniquely prefix usernames in test environments to avoid collisions"],"tags":["database","auth","duplicate","user-management"],"backgroundTag":"duplicate-resource","analyzedSha":"6a27f2decc0b76eb1b54af31849784addb357dbc","analyzedAt":"2026-08-29T20:54:51.419Z","schemaVersion":2},"datasetVersion":"2026-08-29T22:17:34.462Z"}