{"record":{"id":"772b275cd0958bd7","repo":"flipped-aurora/gin-vue-admin","slug":"type","errorCode":null,"errorMessage":"存在相同的type，不允许创建","messagePattern":"存在相同的type，不允许创建","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/sys_dictionary.go","lineNumber":27,"sourceCode":"\t\"github.com/flipped-aurora/gin-vue-admin/server/model/system\"\n\t\"github.com/flipped-aurora/gin-vue-admin/server/model/system/request\"\n\t\"github.com/flipped-aurora/gin-vue-admin/server/utils/logger\"\n\t\"gorm.io/gorm\"\n)\n\n//@author: [piexlmax](https://github.com/piexlmax)\n//@function: CreateSysDictionary\n//@description: 创建字典数据\n//@param: sysDictionary model.SysDictionary\n//@return: err error\n\ntype DictionaryService struct{}\n\nvar DictionaryServiceApp = new(DictionaryService)\n\nfunc (dictionaryService *DictionaryService) CreateSysDictionary(ctx context.Context, sysDictionary system.SysDictionary) (system.SysDictionary, error) {\n\tif (!errors.Is(global.GVA_DB.WithContext(ctx).First(&system.SysDictionary{}, \"type = ?\", sysDictionary.Type).Error, gorm.ErrRecordNotFound)) {\n\t\treturn system.SysDictionary{}, errors.New(\"存在相同的type，不允许创建\")\n\t}\n\t// Create 会把自增主键回写进 sysDictionary,直接返回创建后的实体(含 ID),免去调用方二次回查\n\terr := global.GVA_DB.WithContext(ctx).Create(&sysDictionary).Error\n\treturn sysDictionary, err\n}\n\n//@author: [piexlmax](https://github.com/piexlmax)\n//@function: DeleteSysDictionary\n//@description: 删除字典数据\n//@param: sysDictionary model.SysDictionary\n//@return: err error\n\nfunc (dictionaryService *DictionaryService) DeleteSysDictionary(ctx context.Context, sysDictionary system.SysDictionary) (err error) {\n\terr = global.GVA_DB.WithContext(ctx).Where(\"id = ?\", sysDictionary.ID).Preload(\"SysDictionaryDetails\").First(&sysDictionary).Error\n\tif err != nil && errors.Is(err, gorm.ErrRecordNotFound) {\n\t\treturn errors.New(\"请不要搞事\")\n\t}\n\tif err != nil {","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/sys_dictionary.go#L9-L45","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nawait createDictionary({ name: '状态', type: 'status' })\n// after\nconst existing = await findDictionaryByType('status')\nif (existing) await updateDictionary(existing.ID, { name: '状态' })\nelse await createDictionary({ name: '状态', type: 'status' })","handlingStrategy":"validation","validationCode":"var existing system.SysDictionary\nerr := global.GVA_DB.First(&existing, \"type = ?\", dict.Type).Error\nif !errors.Is(err, gorm.ErrRecordNotFound) {\n    return errors.New(\"dictionary type already exists\")\n}","typeGuard":null,"tryCatchPattern":"created, err := dictService.CreateSysDictionary(ctx, dict)\nif err != nil && err.Error() == \"存在相同的type，不允许创建\" {\n    // query existing by type and update instead\n    return err\n}","preventionTips":["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"],"tags":["dictionary","uniqueness","duplicate","go"],"backgroundTag":"duplicate-key-conflict","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}