flipped-aurora/gin-vue-admin · error
存在相同的type,不允许创建
Error message
存在相同的type,不允许创建
What it means
CreateSysDictionary enforces uniqueness of the dictionary 'type' field. Before inserting, it queries for an existing SysDictionary with the same type; if one is found (query does not return record-not-found), creation is refused. Dictionary type acts as a unique business key.
Source
Thrown at server/service/system/sys_dictionary.go:27
"github.com/flipped-aurora/gin-vue-admin/server/model/system"
"github.com/flipped-aurora/gin-vue-admin/server/model/system/request"
"github.com/flipped-aurora/gin-vue-admin/server/utils/logger"
"gorm.io/gorm"
)
//@author: [piexlmax](https://github.com/piexlmax)
//@function: CreateSysDictionary
//@description: 创建字典数据
//@param: sysDictionary model.SysDictionary
//@return: err error
type DictionaryService struct{}
var DictionaryServiceApp = new(DictionaryService)
func (dictionaryService *DictionaryService) CreateSysDictionary(ctx context.Context, sysDictionary system.SysDictionary) (system.SysDictionary, error) {
if (!errors.Is(global.GVA_DB.WithContext(ctx).First(&system.SysDictionary{}, "type = ?", sysDictionary.Type).Error, gorm.ErrRecordNotFound)) {
return system.SysDictionary{}, errors.New("存在相同的type,不允许创建")
}
// Create 会把自增主键回写进 sysDictionary,直接返回创建后的实体(含 ID),免去调用方二次回查
err := global.GVA_DB.WithContext(ctx).Create(&sysDictionary).Error
return sysDictionary, err
}
//@author: [piexlmax](https://github.com/piexlmax)
//@function: DeleteSysDictionary
//@description: 删除字典数据
//@param: sysDictionary model.SysDictionary
//@return: err error
func (dictionaryService *DictionaryService) DeleteSysDictionary(ctx context.Context, sysDictionary system.SysDictionary) (err error) {
err = global.GVA_DB.WithContext(ctx).Where("id = ?", sysDictionary.ID).Preload("SysDictionaryDetails").First(&sysDictionary).Error
if err != nil && errors.Is(err, gorm.ErrRecordNotFound) {
return errors.New("请不要搞事")
}
if err != nil {View on GitHub (pinned to 3136500ef3)
Solutions
- Pick a unique type value (check existing dictionaries first)
- If the dictionary already exists, edit it instead of creating a new one
- Add a frontend pre-check or rely on the error to surface duplicates; for sync jobs, query-by-type first and update if present
Example fix
// before
await createDictionary({ name: '状态', type: 'status' })
// after
const existing = await findDictionaryByType('status')
if (existing) await updateDictionary(existing.ID, { name: '状态' })
else await createDictionary({ name: '状态', type: 'status' }) Defensive patterns
Strategy: validation
Validate before calling
var existing system.SysDictionary
err := global.GVA_DB.First(&existing, "type = ?", dict.Type).Error
if !errors.Is(err, gorm.ErrRecordNotFound) {
return errors.New("dictionary type already exists")
} Try / catch
created, err := dictService.CreateSysDictionary(ctx, dict)
if err != nil && err.Error() == "存在相同的type,不允许创建" {
// query existing by type and update instead
return err
} Prevention
- Check the dictionary list for the type before creating
- Reset the create form after each successful submission
- For sync jobs, implement query-then-update (upsert) by type
- Consider a DB unique index on type as a backstop
When it happens
Trigger: Creating a dictionary whose Type matches an existing row — e.g. submitting the '新建字典' form with a duplicate type like 'user_status', or a retry of a successful create.
Common situations: Two admins creating the same dictionary concurrently; form didn't reset after a successful create and was resubmitted; API integrations syncing dictionaries without upsert logic.
Related errors
AI-assisted analysis of flipped-aurora/gin-vue-admin@3136500ef3 (2026-08-31).
Data as JSON: /api/errors/772b275cd0958bd7.
Report an issue: GitHub.