{"record":{"id":"975a3d0ce21024be","repo":"flipped-aurora/gin-vue-admin","slug":"api-s-s-w-975a3d","errorCode":null,"errorMessage":"查询自动代码 API %s %s 失败: %w","messagePattern":"查询自动代码 API (.+?) (.+?) 失败: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"server/service/system/auto_code_persistence.go","lineNumber":60,"sourceCode":"}\n\nfunc persistAutoCodeAPIs(tx *gorm.DB, info request.AutoCode, history *request.SysAutoHistoryCreate) error {\n\tif !info.AutoCreateApiToSql || info.OnlyTemplate {\n\t\treturn nil\n\t}\n\tfor _, desired := range info.Apis() {\n\t\tvar existing model.SysApi\n\t\terr := tx.Where(\"path = ? AND method = ?\", desired.Path, desired.Method).First(&existing).Error\n\t\tswitch {\n\t\tcase err == nil:\n\t\t\thistory.ApiIDs = append(history.ApiIDs, existing.ID)\n\t\tcase errors.Is(err, gorm.ErrRecordNotFound):\n\t\t\tif err = tx.Create(&desired).Error; err != nil {\n\t\t\t\treturn fmt.Errorf(\"创建自动代码 API %s %s 失败: %w\", desired.Method, desired.Path, err)\n\t\t\t}\n\t\t\thistory.ApiIDs = append(history.ApiIDs, desired.ID)\n\t\tdefault:\n\t\t\treturn fmt.Errorf(\"查询自动代码 API %s %s 失败: %w\", desired.Method, desired.Path, err)\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc persistAutoCodeMenu(\n\ttx *gorm.DB,\n\tinfo request.AutoCode,\n\tpackageTemplate string,\n\thistory *request.SysAutoHistoryCreate,\n) error {\n\tif !info.AutoCreateMenuToSql {\n\t\treturn nil\n\t}\n\tdesired := info.Menu(packageTemplate)\n\tvar existing model.SysBaseMenu\n\terr := tx.Where(\"name = ?\", desired.Name).First(&existing).Error\n\tswitch {","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/flipped-aurora/gin-vue-admin/blob/3136500ef380842b0eb6c4daa87c3f8a47fcf9e0/server/service/system/auto_code_persistence.go#L42-L78","documentation":"This error is returned by persistAutoCodeAPIs when the tx.Where(\"path = ? AND method = ?\").First(&existing) existence check on sys_api fails with an error other than gorm.ErrRecordNotFound (which is handled as the create-new branch). The code cannot determine whether the API exists, so it fails the transaction, including the method and path in the message for diagnosis.","triggerScenarios":"The SELECT on sys_api fails during auto-code persistence: DB connection dropped mid-transaction, sys_api table missing (migrations not run), unknown-column/schema-drift SQL errors, deadlock or innodb_lock_wait_timeout, or the DB user lacks SELECT privilege on sys_api.","commonSituations":"Fresh deployments where AutoMigrate has not created sys_api; DB user with insufficient privileges; schema mismatch after upgrading gin-vue-admin without migrating; transient network blips between app and DB; long-running transactions hitting lock wait timeouts.","solutions":["Unwrap the error to read the exact DB message (no such table, unknown column, access denied, lock timeout).","Run migrations/AutoMigrate so sys_api exists with path/method columns.","Grant the DB user SELECT privileges on sys_api if access denied.","Retry the persistence operation if the cause is a lock timeout or transient network error.","Shorten transaction scope to reduce lock contention if timeouts recur."],"exampleFix":"// before\ndefault:\n    return fmt.Errorf(\"查询自动代码 API %s %s 失败: %w\", desired.Method, desired.Path, err)\n\n// after\ndefault:\n    logger.Error(\"sys_api lookup failed\",\n        zap.String(\"path\", desired.Path), zap.String(\"method\", desired.Method), zap.Error(err))\n    return fmt.Errorf(\"查询自动代码 API %s %s 失败: %w\", desired.Method, desired.Path, err)","handlingStrategy":"retry","validationCode":"func sysApiTableReady(db *gorm.DB) error {\n    return db.Exec(\"SELECT 1 FROM sys_api LIMIT 1\").Error // surfaces missing table/privilege early\n}","typeGuard":null,"tryCatchPattern":"err := svc.CreateAutoCodeHistory(info)\nif err != nil {\n    if isTransientDBError(err) { // lock wait timeout, connection reset, deadlock\n        return retryWithBackoff(3, func() error { return svc.CreateAutoCodeHistory(info) })\n    }\n    if strings.Contains(fmt.Sprint(err), \"doesn't exist\") {\n        return fmt.Errorf(\"schema not migrated; run AutoMigrate first: %w\", err)\n    }\n    return err\n}","preventionTips":["Run migrations/AutoMigrate as a deployment step before the app serves traffic.","Grant the app DB user SELECT/INSERT privileges on sys_api.","Verify DB connectivity and privileges in a startup readiness check.","Keep transactions short to avoid innodb_lock_wait_timeout under concurrency.","Log unwrapped DB errors to quickly distinguish missing-table vs privilege vs lock issues."],"tags":["database","gorm","query","api","transaction"],"backgroundTag":"db-query-failed","analyzedSha":"3136500ef380842b0eb6c4daa87c3f8a47fcf9e0","analyzedAt":"2026-08-31T13:50:02.721Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}