{"record":{"id":"582bee4f7fe8069b","repo":"flipped-aurora/gin-vue-admin","slug":"w-582bee","errorCode":null,"errorMessage":"查询自动代码重复记录失败: %w","messagePattern":"查询自动代码重复记录失败: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/auto_code_persistence.go","lineNumber":165,"sourceCode":"}\n\nfunc autoCodeIdentityExists(ctx context.Context, db *gorm.DB, info request.AutoCode) (bool, error) {\n\tif db == nil {\n\t\treturn false, errors.New(\"自动代码数据库未初始化\")\n\t}\n\tvar count int64\n\terr := db.WithContext(ctx).Model(&model.SysAutoCodeHistory{}).\n\t\tWhere(\n\t\t\t\"business_db = ? AND (struct_name = ? OR abbreviation = ?) AND package = ? AND flag = ?\",\n\t\t\tinfo.BusinessDB,\n\t\t\tinfo.StructName,\n\t\t\tinfo.Abbreviation,\n\t\t\tinfo.Package,\n\t\t\t0,\n\t\t).\n\t\tCount(&count).Error\n\tif err != nil {\n\t\treturn false, fmt.Errorf(\"查询自动代码重复记录失败: %w\", err)\n\t}\n\treturn count > 0, nil\n}\n","sourceCodeStart":147,"sourceCodeEnd":169,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/auto_code_persistence.go#L147-L169","documentation":"Thrown by autoCodeIdentityExists when the COUNT query that detects an existing auto-code record (same struct/abbreviation/package) fails. The function only returns (true) when count>0; a query error is wrapped so Create can abort instead of silently generating duplicate code.","triggerScenarios":"The GORM Count on the auto-code history/records table errors during Create or the TestAutoCodeIdentityExistsReturnsQueryError test path: missing table, broken DB connection, cancelled context, or SQL syntax error from schema drift.","commonSituations":"Fresh database without migrations for the auto-code history table; DB outage mid-request; tests injecting a failing DB to verify error propagation; wrong datasource in config.","solutions":["Inspect the wrapped %w cause and fix the underlying driver error.","Run migrations to ensure the auto-code history table exists.","Verify DB connectivity/config before running code generation."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if err := db.Exec(\"SELECT 1\").Error; err != nil {\n    return fmt.Errorf(\"database unreachable: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"exists, err := autoCodeIdentityExists(ctx, db, info)\nif err != nil {\n    log.Printf(\"duplicate check failed: %v\", errors.Unwrap(err))\n    return err // do NOT treat as \"not duplicate\"\n}","preventionTips":["Migrate the auto-code history table before generation.","Check DB health in readiness probes.","Never ignore the duplicate-check error and proceed to generate."],"tags":["gorm","database","auto-code","duplicate-check"],"backgroundTag":"db-query-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}