{"record":{"id":"4b2a10c8ca1d542f","repo":"apache/shenyu","slug":"the-user-namespace-rel-is-exist","errorCode":null,"errorMessage":"The user namespace rel is exist！","messagePattern":"The user namespace rel is exist！","errorType":"exception","errorClass":"ShenyuAdminException","httpStatus":null,"severity":"warning","filePath":"shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespaceUserServiceImpl.java","lineNumber":50,"sourceCode":"\nimport java.util.List;\nimport java.util.Objects;\nimport java.util.stream.Collectors;\n\n@Service\npublic class NamespaceUserServiceImpl implements NamespaceUserService {\n    \n    private final NamespaceUserRelMapper namespaceUserRelMapper;\n    \n    public NamespaceUserServiceImpl(final NamespaceUserRelMapper namespaceUserRelMapper) {\n        this.namespaceUserRelMapper = namespaceUserRelMapper;\n    }\n    \n    @Override\n    public NamespaceUserRelVO create(final String namespaceId, final String userId) {\n        NamespaceUserRelDO existNamespaceUserRelDO = namespaceUserRelMapper.selectByNamespaceIdAndUserId(namespaceId, userId);\n        if (!Objects.isNull(existNamespaceUserRelDO)) {\n            throw new ShenyuAdminException(AdminConstants.NAMESPACE_USER_EXIST);\n        }\n        String uuid = UUIDUtils.getInstance().generateShortUuid();\n        NamespaceUserRelDO namespaceUserRelDO = NamespaceUserRelDO.builder()\n                .id(uuid)\n                .namespaceId(namespaceId)\n                .userId(userId)\n                .build();\n        namespaceUserRelMapper.insertSelective(namespaceUserRelDO);\n        \n        return NamespaceUserRelVO.builder()\n                .id(uuid)\n                .namespaceId(namespaceId)\n                .userId(userId)\n                .build();\n    }\n    \n    @Override\n    public List<String> listNamespaceIdByUserId(final String userId) {","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/apache/shenyu/blob/567142e07261b3e615ae8850b30f4421f455cc5d/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/NamespaceUserServiceImpl.java#L32-L68","documentation":"ShenYu admin throws this when creating a namespace-user binding that already exists. NamespaceUserServiceImpl.create() first queries namespaceUserRelMapper.selectByNamespaceIdAndUserId(); if a row with the same (namespaceId, userId) pair is found, it aborts with ShenyuAdminException(AdminConstants.NAMESPACE_USER_EXIST) to keep the relation unique.","triggerScenarios":"Calling POST /namespaceUser (NamespaceUserController -> create -> onNamespaceCreated flow) with a namespaceId and userId that are already linked in the namespace_user_rel table; e.g. clicking 'add user to namespace' twice, or re-submitting a form after a timeout.","commonSituations":"Dashboard double-submit, retry after a slow request that actually succeeded, seeding scripts that run more than once, or admin UI state out of sync with the DB after manual row edits.","solutions":["Check the existing relation first (list users of the namespace or query the namespace_user_rel table) and skip creation if it already exists.","Make the create call idempotent on the caller side: catch ShenyuAdminException with message AdminConstants.NAMESPACE_USER_EXIST and treat it as success.","If the binding is stale or wrong, delete the existing relation before re-creating it via the dashboard or delete API.","Guard UI/forms against double submission (disable button until response)."],"exampleFix":"// before\nnamespaceUserService.create(namespaceId, userId);\n\n// after\ntry {\n    namespaceUserService.create(namespaceId, userId);\n} catch (ShenyuAdminException e) {\n    if (!AdminConstants.NAMESPACE_USER_EXIST.equals(e.getMessage())) {\n        throw e;\n    }\n    // already bound; treat as no-op\n}","handlingStrategy":"try-catch","validationCode":"NamespaceUserRelDO existing = namespaceUserRelMapper.selectByNamespaceIdAndUserId(namespaceId, userId);\nif (existing != null) { /* skip create or update instead */ }","typeGuard":null,"tryCatchPattern":"try {\n    namespaceUserService.create(namespaceId, userId);\n} catch (ShenyuAdminException e) {\n    if (AdminConstants.NAMESPACE_USER_EXIST.equals(e.getMessage())) { /* idempotent no-op */ } else { throw e; }\n}","preventionTips":["Check for an existing (namespaceId, userId) relation before calling create.","Make client-side creation idempotent on this error.","Disable submit buttons while a request is in flight to avoid double submits.","Reconcile UI state with the DB after failed/retried requests."],"tags":["duplicate-record","admin-api","namespace","uniqueness"],"backgroundTag":"file-already-exists","analyzedSha":"567142e07261b3e615ae8850b30f4421f455cc5d","analyzedAt":"2026-09-12T10:08:21.293Z","contentChangedAt":"2026-09-12T10:08:21.293Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}