flipped-aurora/gin-vue-admin · warning · ErrRoleExistence
存在相同角色id
Error message
存在相同角色id
What it means
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.
Source
Thrown at server/service/system/sys_authority.go:19
package system
import (
"context"
"errors"
"strconv"
systemReq "github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
"github.com/flipped-aurora/gin-vue-admin/server/global"
"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
"github.com/flipped-aurora/gin-vue-admin/server/model/system/response"
"github.com/flipped-aurora/gin-vue-admin/server/utils/datascope"
"github.com/flipped-aurora/gin-vue-admin/server/utils/logger"
"gorm.io/gorm"
)
var ErrRoleExistence = errors.New("存在相同角色id")
//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateAuthority
//@description: 创建一个角色
//@param: auth model.SysAuthority
//@return: authority system.SysAuthority, err error
type AuthorityService struct{}
var AuthorityServiceApp = new(AuthorityService)
func (authorityService *AuthorityService) CreateAuthority(ctx context.Context, auth system.SysAuthority) (authority system.SysAuthority, err error) {
if err = global.GVA_DB.WithContext(ctx).Where("authority_id = ?", auth.AuthorityId).First(&system.SysAuthority{}).Error; !errors.Is(err, gorm.ErrRecordNotFound) {
return auth, ErrRoleExistence
}
e := global.GVA_DB.WithContext(ctx).Transaction(func(tx *gorm.DB) error {View on GitHub (pinned to 3136500ef3)
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.
Example fix
// before
copyAuthority({ authority: { authorityId: 888, authorityName: 'copy of admin' } }) // 888 exists
// after
copyAuthority({ authority: { authorityId: 8881, authorityName: 'copy of admin' } }) // unique ID Defensive patterns
Strategy: try-catch
Validate before calling
const exists = (await getAuthorityList()).data.list.some(r => r.authorityId === form.authorityId)
if (exists) throw new Error('角色 ID ' + form.authorityId + ' 已存在,请换一个 ID 或改用编辑') Try / catch
try {
await createAuthority(form)
} catch (e) {
if (errors.Is(e, system.ErrRoleExistence) || String(e?.msg).includes('存在相同角色id')) {
ElMessage.warning('角色 ID 已存在,请使用唯一 ID 或直接编辑现有角色')
} else { throw e }
} Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/aed6ef58bb8ff7ef.
Report an issue: GitHub.