{"record":{"id":"aed6ef58bb8ff7ef","repo":"flipped-aurora/gin-vue-admin","slug":"id-aed6ef","errorCode":null,"errorMessage":"存在相同角色id","messagePattern":"存在相同角色id","errorType":"validation","errorClass":"ErrRoleExistence","httpStatus":null,"severity":"warning","filePath":"server/service/system/sys_authority.go","lineNumber":19,"sourceCode":"package system\n\nimport (\n\t\"context\"\n\t\"errors\"\n\t\"strconv\"\n\n\tsystemReq \"github.com/flipped-aurora/gin-vue-admin/server/model/system/request\"\n\n\t\"github.com/flipped-aurora/gin-vue-admin/server/global\"\n\t\"github.com/flipped-aurora/gin-vue-admin/server/model/common/request\"\n\t\"github.com/flipped-aurora/gin-vue-admin/server/model/system\"\n\t\"github.com/flipped-aurora/gin-vue-admin/server/model/system/response\"\n\t\"github.com/flipped-aurora/gin-vue-admin/server/utils/datascope\"\n\t\"github.com/flipped-aurora/gin-vue-admin/server/utils/logger\"\n\t\"gorm.io/gorm\"\n)\n\nvar ErrRoleExistence = errors.New(\"存在相同角色id\")\n\n//@author: [piexlmax](https://github.com/piexlmax)\n//@function: CreateAuthority\n//@description: 创建一个角色\n//@param: auth model.SysAuthority\n//@return: authority system.SysAuthority, err error\n\ntype AuthorityService struct{}\n\nvar AuthorityServiceApp = new(AuthorityService)\n\nfunc (authorityService *AuthorityService) CreateAuthority(ctx context.Context, auth system.SysAuthority) (authority system.SysAuthority, err error) {\n\n\tif err = global.GVA_DB.WithContext(ctx).Where(\"authority_id = ?\", auth.AuthorityId).First(&system.SysAuthority{}).Error; !errors.Is(err, gorm.ErrRecordNotFound) {\n\t\treturn auth, ErrRoleExistence\n\t}\n\n\te := global.GVA_DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/sys_authority.go#L1-L37","documentation":"ErrRoleExistence (\"存在相同角色id\") is a package-level sentinel error thrown when creating or copying an authority whose authority_id already exists in sys_authorities. Both CreateAuthority and CopyAuthority check for an existing row with the target ID first and return this error to keep role IDs unique.","triggerScenarios":"POST /authority/createAuthority or copyAuthority with an authorityId that already exists — e.g. copying role 888 without changing the generated new ID, or manually specifying an existing ID like 9528.","commonSituations":"Importing role seed data twice; a copy operation where the frontend failed to assign a fresh authorityId; restoring a DB dump over an environment that already has the roles; syncing roles between environments with overlapping IDs.","solutions":["Use a different authorityId (choose a number not present in sys_authorities).","If the role already exists and you meant to modify it, call updateAuthority instead of create.","Delete the conflicting role first if it is obsolete and has no dependent users/children, then create."],"exampleFix":"// before\ncopyAuthority({ authority: { authorityId: 888, authorityName: 'copy of admin' } }) // 888 exists\n\n// after\ncopyAuthority({ authority: { authorityId: 8881, authorityName: 'copy of admin' } }) // unique ID","handlingStrategy":"try-catch","validationCode":"const exists = (await getAuthorityList()).data.list.some(r => r.authorityId === form.authorityId)\nif (exists) throw new Error('角色 ID ' + form.authorityId + ' 已存在，请换一个 ID 或改用编辑')","typeGuard":null,"tryCatchPattern":"try {\n  await createAuthority(form)\n} catch (e) {\n  if (errors.Is(e, system.ErrRoleExistence) || String(e?.msg).includes('存在相同角色id')) {\n    ElMessage.warning('角色 ID 已存在，请使用唯一 ID 或直接编辑现有角色')\n  } else { throw e }\n}","preventionTips":["Let the system assign/derive a fresh authorityId on copy instead of reusing the source ID.","Validate copied role payloads: strip or regenerate the ID before submit.","Keep role IDs unique across seed files and environment dumps.","Check for max(existing id)+1 style collisions when manually numbering roles."],"tags":["duplicate-record","role","authority","validation"],"backgroundTag":"duplicate-unique-key","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}